使用PIL(Python-Imaging)反轉圖像的顏色方法
利用PIL將圖片轉換為黑色與白色反轉的圖片,下面筆者小白介紹如何實現(xiàn)。
解決方案一:
from PIL import Image import PIL.ImageOps #讀入圖片 image = Image.open('your_image.png') #反轉 inverted_image = PIL.ImageOps.invert(image) #保存圖片 inverted_image.save('new_name.png')
注意:“ImageOps模塊包含多個'ready-made'圖像處理操作,該模塊有些實驗性,大多數(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')
解決方案三:
注:對于使用”1″模式的圖像(即,1位像素,黑白色,以每個字節(jié)為單位存儲的see docs),您需要在調用PIL.ImageOps.invert之前將其轉換為”L”模式。
im = im.convert('L') im = ImageOps.invert(im) im = im.convert('1')
以上這篇使用PIL(Python-Imaging)反轉圖像的顏色方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- Python圖像處理庫PIL詳細使用說明
- python使用pil進行圖像處理(等比例壓縮、裁剪)實例代碼
- Python圖像處理庫PIL的ImageDraw模塊介紹詳解
- 在Python中使用PIL模塊處理圖像的教程
- Python圖像處理庫PIL的ImageFont模塊使用介紹
- Python編程中使用Pillow來處理圖像的基礎教程
- Python Pillow.Image 圖像保存和參數(shù)選擇方式
- Python用Pillow(PIL)進行簡單的圖像操作方法
- Python圖像處理庫PIL的ImageGrab模塊介紹詳解
- Python圖像處理PIL各模塊詳細介紹(推薦)
- Python PIL讀取的圖像發(fā)生自動旋轉的實現(xiàn)方法
- Python圖像處理庫PIL的ImageEnhance模塊使用介紹
- 詳解python opencv、scikit-image和PIL圖像處理庫比較
- python PIL Image 圖像處理基本操作實例
相關文章
將tensorflow.Variable中的某些元素取出組成一個新的矩陣示例
今天小編就為大家分享一篇將tensorflow.Variable中的某些元素取出組成一個新的矩陣示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01【Python】Python的urllib模塊、urllib2模塊批量進行網(wǎng)頁下載文件
這篇文章主要介紹了Python的urllib模塊、urllib2模塊批量進行網(wǎng)頁下載文件,就是一個簡單的從網(wǎng)頁抓取數(shù)據(jù)、下載文件的小程序,需要的可以了解一下。2016-11-11