這篇文章將為大家詳細(xì)講解有關(guān)python中如何使用PIL反轉(zhuǎn)圖像的顏色方法,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
解決方案一:
from PIL import Image import PIL.ImageOps #讀入圖片 image = Image.open('your_image.png') #反轉(zhuǎn) inverted_image = PIL.ImageOps.invert(image) #保存圖片 inverted_image.save('new_name.png')
注意:“ImageOps模塊包含多個(gè)'ready-made'圖像處理操作,該模塊有些實(shí)驗(yàn)性,大多數(shù)操作符只適用于L和RGB圖像?!?/p>
解決方案二:
如果圖像是RGBA透明的,參考如下代碼。
from PIL import Image import PIL.ImageOps image = Image.open('your_image.png') if image.mode == 'RGBA': r,g,b,a = image.split() rgb_image = Image.merge('RGB', (r,g,b)) inverted_image = PIL.ImageOps.invert(rgb_image) r2,g2,b2 = inverted_image.split() final_transparent_image = Image.merge('RGBA', (r2,g2,b2,a)) final_transparent_image.save('new_file.png') else: inverted_image = PIL.ImageOps.invert(image) inverted_image.save('new_name.png')
解決方案三:
注:對(duì)于使用”1″模式的圖像(即,1位像素,黑白色,以每個(gè)字節(jié)為單位存儲(chǔ)的see docs),您需要在調(diào)用PIL.ImageOps.invert之前將其轉(zhuǎn)換為”L”模式。
im = im.convert('L') im = ImageOps.invert(im) im = im.convert('1')
關(guān)于“python中如何使用PIL反轉(zhuǎn)圖像的顏色方法”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
網(wǎng)頁(yè)名稱:python中如何使用PIL反轉(zhuǎn)圖像的顏色方法-創(chuàng)新互聯(lián)
當(dāng)前網(wǎng)址:http://vcdvsql.cn/article6/ddgiig.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、定制開發(fā)、品牌網(wǎng)站設(shè)計(jì)、電子商務(wù)、網(wǎng)站內(nèi)鏈、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容