如何在python3中使用PIL将图片转换成RGB图片

发布时间:2021-02-26 15:37:09 作者:戴恩恩
来源:亿速云 阅读:478

这篇文章主要介绍了如何在python3中使用PIL将图片转换成RGB图片,亿速云小编觉得不错,现在分享给大家,也给大家做个参考,一起跟随亿速云小编来看看吧!

python可以做什么

Python是一种编程语言,内置了许多有效的工具,Python几乎无所不能,该语言通俗易懂、容易入门、功能强大,在许多领域中都有广泛的应用,例如最热门的大数据分析,人工智能,Web开发等。

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

以上就是亿速云小编为大家收集整理的如何在python3中使用PIL将图片转换成RGB图片,如何觉得亿速云网站的内容还不错,欢迎将亿速云网站推荐给身边好友。

推荐阅读:
  1. 使用Python将PDF转换成图片
  2. php怎么将png格式图片转换成jpg格式图片

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python3 pil

上一篇:怎么在python中对链表进行反转

下一篇:怎么在Python中使用opencv对视频与图片进行转换

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》