PIL图像处理标准库——convert('RGB')

在pytorch自定义数据集的时候,需要使用到如下代码

Image.open(x).convert('RGB')

使用Image.open读出图像即可,为什么还需要使用convert('RGB')转换成RGB,难道Image.open()读出的彩色图像不是RGB吗

使用如下代码进行测试:

img = Image.open('pokeman\\bulbasaur\\00000000.png').convert('RGB')
img2 = Image.open('pokeman\\bulbasaur\\00000000.png')
print(img)
print(img2)
print(img.shape)
print(img2.shape)

输出结果:

<PIL.Image.Image image mode=RGB size=900x900 at 0x15D0609AF28>
<PIL.PngImagePlugin.PngImageFile image mode=RGBA size=900x900 at 0x15D08BD6550>
(3, 900, 900)
(4, 900, 900)

可以看到,如果不使用.convert('RGB')进行转换的话,读出来的图像是RGBA四通道的,A通道为透明通道,该对深度学习模型训练来说暂时用不到,因此使用convert('RGB')进行通道转换。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章