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

原文地址:https://www.cnblogs.com/marsggbo/p/15530819.html