Python如何将8位的图片转为24位的图片

Python如何将8位的图片转为24位的图片

这篇文章主要介绍Python如何将8位的图片转为24位的图片,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

用的pytorch来训练deeplabv3+

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

8位的:

Python如何将8位的图片转为24位的图片

24位的:

Python如何将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位的图片”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!