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

yipeiwu_com6年前Python基础

用的pytorch来训练deeplabv3+

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

8位的:

在这里插入图片描述

24位的:

在这里插入图片描述

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

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

#!/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位的图片,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对【听图阁-专注于Python设计】网站的支持!

相关文章

python中文乱码的解决方法

乱码原因: 源码文件的编码格式为utf-8,但是window的本地默认编码是gbk,所以在控制台直接打印utf-8的字符串当然是乱码了! 解决方法: 1、print mystr.deco...

Python中请使用isinstance()判断变量类型

一、isinstance() 在Python中可以使用type()与isinstance()这两个函数判断对象类型,而isinstance()函数的使用上比type更加方便。 复制代码...

PyCharm的设置方法和第一个Python程序的建立

PyCharm的设置方法和第一个Python程序的建立

1.代码编辑 字体大小设置 进入 File—》Settings—》Editor—》Colors & Fonts—》Font中。 首先要点击“Save as”然后为这个设置命名,我这里填入...

python @property的用法及含义全面解析

在接触python时最开始接触的代码,取长方形的长和宽,定义一个长方形类,然后设置长方形的长宽属性,通过实例化的方式调用长和宽,像如下代码一样。 class Rectangle(ob...

Python 窗体(tkinter)按钮 位置实例

如下所示: import tkinter def go(): #函数 print("go函数") win=tkinter.Tk() #构造窗体 win.title("he...