python3用PIL把图片转换为RGB图片的实例
作者:农民小飞侠 时间:2021-08-20 02:08:49
感想
我们在做深度学习处理图片的时候,如果是自己制作或者收集的数据集,不可避免的要对数据集进行处理,然后大多数模型都只支持RGB格式的图片,这个时候,我们需要把其他格式的图片,例如灰度图像转换为RGB的图片,网上只有灰度图像转换为RGB的教程,我这里弥补一下空缺。
from PIL import Image
import numpy as np
L_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 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=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||sculpture
train/5020991.jpg* en el cuadro de honor de todas las 50appsalud en un grfico en espaol* mhealth
train/3525659.jpg* information mogadishu port expansion turkish company* somalia
来源:https://blog.csdn.net/w5688414/article/details/84798844
标签:python3,PIL,图片,转换,RGB
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python strip()函数 介绍
2023-06-15 11:59:47
python生成器/yield协程/gevent写简单的图片下载器功能示例
2022-02-25 10:08:03
python实现扫描局域网指定网段ip的方法
2023-09-28 16:44:45
Python字符串格式化输出方法分析
2022-03-24 22:18:45
python之DataFrame实现excel合并单元格
2021-02-22 04:45:00
![](https://img.aspxhome.com/file/2023/7/109557_0s.png)
php实现的验证码文件类实例
2023-08-17 17:54:52
CSS网页设计时关于字体大小的设计
2008-10-23 13:42:00
python 实现超级玛丽游戏
2023-10-10 09:38:38
![](https://img.aspxhome.com/file/2023/9/70049_0s.gif)
Python os.mkdir()与os.makedirs()的使用区别
2023-08-19 20:01:56
通过SQL Server的位运算功能巧妙解决多选查询方法
2012-01-29 17:54:35
django 使用 request 获取浏览器发送的参数示例代码
2023-07-26 15:04:44
各种JavaScript开发工具比较
2007-10-23 13:29:00
![](/images/sg_trans.gif)
python爬虫 urllib模块url编码处理详解
2021-09-13 02:24:37
python 实现敏感词过滤的方法
2022-08-09 10:22:51
数据库性能优化之冗余字段的作用
2011-03-03 19:21:00
ASP 隐藏下载地址及防盗链代码
2011-02-26 11:17:00
python+opencv图像分割实现分割不规则ROI区域方法汇总
2021-04-25 01:50:54
对django中foreignkey的简单使用详解
2021-09-04 21:04:20
Python如何匹配文本并在其上一行追加文本
2022-10-19 13:16:43
![](https://img.aspxhome.com/file/2023/2/99122_0s.png)
请站在用户的角度上说话
2009-05-12 12:03:00