Python Pillow Image Examples

The Image class is the most important class in the Pillow library and is defined in the Image module. Using the Image class, you can instantiate an Image object and process the image by calling a series of properties and methods on the object. This article will tell you how to use the Pillow Image class with examples.

1. How To Import The Pillow Image Module.

  1. We can import the pillow image module using the below commands.
    from PIL import Image

2. How To Create The Pillow Image Object.

  1. Pilow provides two methods to create image instances, they are open() & new().

2.1 Using Image Class’s open() Method To Read An Image File.

  1. Using the open() method of the Image class, you can create an image object. The syntax format is as follows.
    img = Image.open(fp,mode="r")
  2. fp: the abbreviation of the “file path”, which indicates the image file path in string format.
  3. mode: optional parameter. If this parameter is specified, it must be set to “r”, otherwise, the ValueError exception will be thrown.
  4. Below is the example source code.
    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 Using Image Class’s new() Method To Create An Image File.

  1. Use the new() method provided by the Image class to create a new image object. The syntax format is as follows.
    img = Image.new(mode,size,color)
  2. mode: image mode, string format parameters, such as ‘RGB’ (true color image), ‘L‘ (gray image), ‘CMYK‘ (color map printing mode), etc.
  3. size: image size, the tuple parameter (width, height) represents the pixel size of the image.
  4. color: image color, the default value is 0, which means black. The parameter value supports (R, G, B) triple number format, the hexadecimal value of color, and the English word of color.
  5. Below is the example of the Image class’s new() method, it will first use the new() method to create a new Image object, and then show it on the screen, and then save it to a local image file.
    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 Class Attributes.

  1. format: return the image format.
  2. info: return the image-related information.
  3. mode: return the image mode, below list some commonly used image modes.
    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: returns whether the image is read-only or not, 1 means read-only, 0 means read-write.
  5. size: return the image size.
  6. Below is the example of the above pillow Image class’s attributes.
    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. Below is the above example execution output.
    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)
0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Clicky
0
Would love your thoughts, please comment.x
()
x