在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')進行通道轉換。