脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python将8位的图片转为24位的图片实现方法

Python将8位的图片转为24位的图片实现方法

2021-04-12 00:26生如 Python

这篇文章主要介绍了Python将8位的图片转为24位的图片的实现代码,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下

用的pytorch来训练deeplabv3+

在做deeplabv3+的过程中,我的训练图片是8位的,如下图:

8位的:

Python将8位的图片转为24位的图片实现方法

24位的:

Python将8位的图片转为24位的图片实现方法

这样虽然在训练过程中能够正常训练。但是在评估过程中会出错,所以决定将训练图片转成24位图,重新训练。最后结果也表明了,只要将训练图片转成24位后之后的评估可视化等都没有问题。

由于rgb的图片就为24位,则简单将图片利用pil转为rgb格式即可

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
created on tue oct 24 10:47:36 2018
@author: yxh
"""
import numpy as np
from pil import image
import matplotlib.pyplot as plt
import os
import sys
import shutil
path='/home/yxh/caffe/examples/fcn/images/images/'
newpath='/home/yxh/caffe/examples/fcn/images/output/'
def turnto24(path):
 filelist = []
 files = os.listdir(path)
 i=0
 for f in files:
  imgpath = path + '/' +f
  img=image.open(f).convert('rgb')
  dirpath = newpath
  file_name, file_extend = os.path.splitext(f)
  dst = os.path.join(os.path.abspath(dirpath), file_name + '.jpg')
  img.save(dst)
turnto24(path)

总结

以上所述是小编给大家介绍的python将8位的图片转为24位的图片,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:https://blog.csdn.net/qq_17130909/article/details/83340804

延伸 · 阅读

精彩推荐