zl程序教程

您现在的位置是:首页 >  Python

当前栏目

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

2023-03-14 22:31:29 时间

对原文有修改: 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()

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

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