python3用PIL把图片转换为RGB图片的实例
Python  /  管理员 发布于 7年前   207
感想
我们在做深度学习处理图片的时候,如果是自己制作或者收集的数据集,不可避免的要对数据集进行处理,然后大多数模型都只支持RGB格式的图片,这个时候,我们需要把其他格式的图片,例如灰度图像转换为RGB的图片,网上只有灰度图像转换为RGB的教程,我这里弥补一下空缺。
from PIL import Imageimport numpy as npL_path='train/5509031.jpg'L_image=Image.open(L_path)out = L_image.convert("RGB")img=np.array(out)print(out.mode)print(out.size)print(img.shape)
然后就可以转换了哈。
如果是大量的图片呢,那就笨办法,用循环判断吧:
from PIL import Imagefrom tqdm import tqdmimport numpy as nproot_path='data'for item in tqdm(examples): arr=item.strip().split('*') img_name=arr[0] image_path=os.path.join(root_path,img_name) img=Image.open(image_path) if(img.mode!='RGB'): img = img.convert("RGB") img=np.array(img) print(img_name) print(img.shape) # add your code
我的图片路径是通过一个txt文件读取的,这里给出一些train.txt里面样例:
train/1769512.jpg* postcard construction 67 mixed media epoxy collage 7 x 135 x 4* art||drawing||sculpturetrain/5020991.jpg* en el cuadro de honor de todas las 50appsalud en un grfico en espaol* mhealthtrain/3525659.jpg* information mogadishu port expansion turkish company* somalia
以上这篇python3用PIL把图片转换为RGB图片的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号