溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何在python3中使用PIL將圖片轉(zhuǎn)換成RGB圖片

發(fā)布時(shí)間:2021-02-26 15:37:09 來(lái)源:億速云 閱讀:495 作者:戴恩恩 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹了如何在python3中使用PIL將圖片轉(zhuǎn)換成RGB圖片,億速云小編覺(jué)得不錯(cuò),現(xiàn)在分享給大家,也給大家做個(gè)參考,一起跟隨億速云小編來(lái)看看吧!

python可以做什么

Python是一種編程語(yǔ)言,內(nèi)置了許多有效的工具,Python幾乎無(wú)所不能,該語(yǔ)言通俗易懂、容易入門(mén)、功能強(qiáng)大,在許多領(lǐng)域中都有廣泛的應(yīng)用,例如最熱門(mén)的大數(shù)據(jù)分析,人工智能,Web開(kāi)發(fā)等。

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)

然后就可以轉(zhuǎn)換了哈。

如果是大量的圖片呢,那就笨辦法,用循環(huán)判斷吧:

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

我的圖片路徑是通過(guò)一個(gè)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將圖片轉(zhuǎn)換成RGB圖片,如何覺(jué)得億速云網(wǎng)站的內(nèi)容還不錯(cuò),歡迎將億速云網(wǎng)站推薦給身邊好友。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI