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 rstrip()方法实例详解

Python 字符串 描述 Python rstrip() 删除 string 字符串末尾的指定字符(默认为空格). 语法 rstrip()方法语法: str.rstrip([chars...

解决在pycharm中显示额外的 figure 窗口问题

解决在pycharm中显示额外的 figure 窗口问题

问题描述 在电脑中重新安装Anaconda3&PyCharm后,运行原来的程序画图时出现了下图界面。 不能弹出如下图所示的“figure”窗口。 解决方法: 这是因为PyCharm在...

Python 条件判断的缩写方法

return (1==1) ? "is easy" : "my god" //C...

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

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

Python requests模块实例用法

1、Requests模块说明 Requests 是使用 Apache2 Licensed 许可证的 HTTP 库。用 Python 编写,真正的为人类着想。 Python 标准库中的 u...