感想
我們在做深度學習處理圖檔的時候,如果是自己制作或者收集的資料集,不可避免的要對資料集進行處理,然後大多數模型都隻支援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
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