如何使用Python 枕头图像实例

158 阅读3分钟

图像类是Pillow库中最重要的类,被定义在图像模块中。使用Image类,你可以实例化一个Image对象,并通过调用该对象的一系列属性和方法来处理图像。本文将通过实例告诉你如何使用Pillow图像类。

1.如何导入Pillow Image模块

  1. 我们可以用下面的命令导入枕头图像模块。

    from PIL import Image
    

2.如何创建Pillow图像对象

  1. Pilow提供两种方法来创建图像实例,它们是open()new()

2.1 使用图像类的open()方法来读取图像文件。

  1. 使用图像类的**open()**方法,你可以创建一个图像对象。语法格式如下。

    img = Image.open(fp,mode="r")
    
  2. fp:"文件路径 "的缩写,它以字符串格式表示图像文件的路径。

  3. mode:可选参数。如果指定了这个参数,它必须被设置为**"r",否则将产生ValueError**异常。

  4. 下面是例子的源代码:

    from PIL import Image
    
    
    def pillow_read_image_example():
        
        file_path = '/Users/songzhao/Desktop/set-matplotlib-plot-axis-range.webp'
        
        # open the image file use the Image class's open() method.
        img = Image.open(fp = file_path)
    
        # call the Image object's show() method to display the image.
        img.show()
    
    
    if __name__ == '__main__':
        
        pillow_read_image_example()
    

2.2 使用图像类的new()方法来创建一个图像文件

  1. 使用Image类提供的**new()**方法来创建一个新的图像对象。语法格式如下。

    img = Image.new(mode,size,color)
    
  2. mode:图像模式,字符串格式参数,如'RGB'(真彩色图像)、'L'(灰色图像)、'CMYK'(彩色地图打印模式),等等。

  3. size:图像大小,元组参数(宽、高)代表图像的像素大小。

  4. color:图像颜色,默认值为0,即为黑色。参数值支持(R、G、B)三数格式,颜色的十六进制值,以及颜色的英文单词。

  5. 下面是Image类的new()方法的例子,它将首先使用new()方法创建一个新的Image对象,然后在屏幕上显示,再保存到本地图像文件中。

    from PIL import Image
    
    def pillow_create_new_image_example():    
        
        # create an Image object use the Image class's new() method.
        img1 = Image.new(mode='RGB', size=(300,150), color="green")
        
        # display the Image object.
        img1.show()
        
        # define the saved file path.
        file_path = '/Users/songzhao/Desktop/test.webp'
        
        # save the image to the above file.
        img1.save(fp = file_path)
    
    
    if __name__ == '__main__':
        
        pillow_create_new_image_example()
    

3.Python Pillow Image类的属性

  1. format:返回图像格式。

  2. info:返回与图像相关的信息。

  3. mode:返回图像模式,下面列出了一些常用的图像模式。

    1: 1 bit pixel (value range 0-1), 0 means black, 1 means white, monochromatic channel.
    
    CMYK: 4 x 8 bit pixels, four color channel, can be adapted to print pictures.
    
    F: 32-bit floating point pixel, monochrome channel.
    
    HSV: 3 x 8 bit pixels, hue, saturation, value color space, tri-color channel.
    
    I: 32-bit signed integer pixels, monochrome channel.
    
    L: 8-bit pixel (value range 0-255), grayscale, monochrome channel.
    
    LAB: 3 x 8-bit pixels, L * a * b color space, tri-color channel.
    
    P: 8 bit pixels, use the palette to map to any other mode, monochrome channel.
    
    RGB: 3 x 8-bit pixels, true color, three-color channels, value range of each channel is 0-255.
    
    RGBA: 4 x 8 bit pixels, true color + transparent channel, four color channel.
    
    YCbCr: 3 x 8 bit pixels, color video format, tri-color channel.
    
  4. readonly:返回图像是否为只读,1表示只读,0表示读写。

  5. size:返回图像大小。

  6. 下面是上述枕头图像类的属性示例。

    from PIL import Image
    
    def pillow_show_image_attributes():
        
        file_path = '/Users/songzhao/Desktop/set-matplotlib-plot-axis-range.png'
        
        # open the image file use the Image class's open() method.
        img = Image.open(fp = file_path)
        
        # get the image format.
        fmt = img.format    
        print('The image format is ', fmt)
        
        # get the image info.
        info = img.info
        print('The image info is ', info)
        
        # get the image color mode.
        mode = img.mode
        print('The image mode is ', mode)
        
        # get the image readonly attribute.
        read_only  = img.readonly
        print('The image is readonly  ', read_only)
        
        # get the image size.
        size  = img.size
        print('The image size  is ', size)
        
    
    
    if __name__ == '__main__':
        
        pillow_show_image_attributes()
    
    
  7. 下面是上述例子的执行输出。

    The image format is  PNG
    The image info is  {'Software': 'Matplotlib version3.3.4, https://matplotlib.org/', 'dpi': (100, 100)}
    The image mode is  RGBA
    The image is readonly   1
    The image size  is  (1138, 484)