当前位置 博文首页 > qq_43614355的博客:Python图像处理 PIL中convert(‘L‘)函数原

    qq_43614355的博客:Python图像处理 PIL中convert(‘L‘)函数原

    作者:[db:作者] 时间:2021-09-02 10:19

    • img.convert(mode=None, matrix=None, dither=None, palette=0, colors=256)
       
        PIL有九种不同模式: 1,L,P,RGB,RGBA,CMYK,YCbCr,I,F。

    参数解释:
    param mode:请求的模式。参见:概念模式。

      matrix:可选的转换矩阵。如果给定,则应为包含浮点值的4元组或12元组。 
      
      dither:抖动方法,在从模式“RGB”转换为“ P”或从“ RGB”或“ L”转换为“1”时使用。可用的方法有:data:`NONE`或:data:`FLOYDSTEINBERG`(默认)。请注意,在提供``matrix''时不使用此选项。
      
      palette:从模式“ RGB”转换为“ P”时使用的调色板。可用的调色板是WEB或ADAPTIVE。 
      
      colors:用于“ ADAPTIVE”调色板的颜色数。 默认值为256。
    

    1.1 img.convert(‘1’)
      为二值图像,非黑即白。每个像素用8个bit表示,0表示黑,255表示白。

    1.1.1 Code

    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。

    1.2.1 Code

    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.3 img.convert(‘P’)
    1.3.1 Code

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

    剩下的集中感兴趣的小伙伴可以自己试一下

    cs