python 3はPILで画像をRGB画像に変換した例です。


感想
私達は深さ学習の処理画像を作る時、自分でデータセットを作ったり収集したりする場合、避けられないのはデータセットを処理して、そしてほとんどのモデルは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ファイルを通して読みました。ここでいくつかのtrin.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

以上のpython 3はPILで画像をRGB画像に変換した例は、小編集が皆さんに共有した内容の全てです。参考にしていただければ幸いです。よろしくお願いします。