Python圖像處理 PIL中convert函數的mode總結


對原文有修改: https://www.cnblogs.com/haifwu/p/12825741.html

1. img = img.convert()

PIL有九種不同模式: 1LPRGBRGBACMYKYCbCrIF

1.1 img.convert('1')

為二值圖像,非黑即白。每個像素用8個bit表示,0表示黑,255表示白。

代碼示例

from PIL import Image

def convert_1():
    image = Image.open("D:/pytorch_code/pytorch_study/fusion_datasets/1.jpg")
    image_1 = image.convert('1')
    image.show()
    image_1.show()

image

1.2 img.convert('L')

轉化為灰度圖像,每個像素用8個bit表示,0表示黑,255表示白,其他數字表示不同的灰度。

轉換公式:L = R * 299/1000 + G * 587/1000+ B * 114/1000。

代碼示例

from PIL import Image

def convert_L():
    image = Image.open("D:/pytorch_code/pytorch_study/fusion_datasets/1.jpg")
    image_L = image.convert('L')
    image.show()
    image_L.show()

image

對比上圖可以發現,1模式得到圖頓點很多,有點像高斯噪聲的感覺,而L模式更平滑一些。

1.3 img.convert('P')

代碼示例

from PIL import Image

def convert_P():
    image = Image.open("D:/pytorch_code/pytorch_study/fusion_datasets/1.jpg")
    image_P = image.convert('P')
    image.show()
    image_P.show()

image


微信公眾號:AutoML機器學習
MARSGGBO原創
如有意合作或學術討論歡迎私戳聯系~
郵箱:marsggbo@foxmail.com


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM