• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

python3用PIL把图片转换为RGB图片的实例

python 搞代码 4年前 (2022-01-07) 16次浏览 已收录 0个评论

今天小编就为大家分享一篇python3用PIL把图片转换为RGB图片的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

感想

我们在做深度学习处理图片的时候,如果是自己制作或者收集的数据集,不可避免的要对数据集进行处理,然后大多数模型都只支持RGB格式的图片,这个时候,我们需要把其他格式的图片,例如灰度图像转换为RGB的图片,网上只有灰度图像转换为RGB的教程,我这里弥补一下空缺。

 from PIL import Image import numpy as np L_path='train/5509031.jpg-600' 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 Image from tqdm import tqdm import numpy as np root_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=n<mark style="color:transparent">来源gaodaimacom搞#^代%!码&网</mark>p.array(img) print(img_name) print(img.shape) # add your code

我的图片路径是通过一个txt文件读取的,这里给出一些train.txt里面样例:

 train/1769512.jpg-600* postcard construction 67 mixed media epoxy collage 7 x 135 x 4* art||drawing||sculpture train/5020991.jpg-600* en el cuadro de honor de todas las 50appsalud en un grfico en espaol* mhealth train/3525659.jpg-600* information mogadishu port expansion turkish company* somalia 

以上这篇python3用PIL把图片转换为RGB图片的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持gaodaima搞代码网

以上就是python3用PIL把图片转换为RGB图片的实例的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:python3用PIL把图片转换为RGB图片的实例

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址