1. img = img.convert()

PIL有九种不同模式: ​​1​​,​​L​​,​​P​​,​​RGB​​,​​RGBA​​,​​CMYK​​,​​YCbCr​​,​​I​​,​​F​​。

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()


Python图像处理 PIL中convert函数的mode总结_二值图像

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()


Python图像处理 PIL中convert函数的mode总结_PIL_02

对比上图可以发现,​​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()


Python图像处理 PIL中convert函数的mode总结_二值图像_03



微信公众号:AutoML机器学习

Python图像处理 PIL中convert函数的mode总结_PIL_04MARSGGBO♥原创