Python image processing_ Pilot module

1, Pilot description

The pilot module provides a wide range of file format support, efficient internal representation and very powerful Image processing functions, the core of which is the Image module

2, Common sub modules of the pilot module

Sub moduleexplain
ImageThe Image module provides a class with the same name to represent PIL images. The module also provides many factory functions, including loading images from files and creating new images
ImageDrawThe ImageDraw module provides simple two-dimensional (2D) graphics for image objects. You can use this module to create new images, annotate or modify existing images, and dynamically generate images for Web use
ImageGrabThe ImageGrab module can be used to copy the contents of the screen or clipboard to the PIL image memory
ImageFilterThe ImageFilter module contains a predefined set of filters that can be used with the Image.filter() method
ImageColorThe ImageColor module contains a color table and a converter from CSS3 style color specifiers to RGB tuples. This module is used by modules such as PIL.Image.new() and ImageDraw
ImageFontThe ImageFont module defines a class with the same name, which is used in conjunction with the PIL.ImageDraw.Draw.text() method

3, Image modes supported by the pilot module

Image modeexplain
1Black and white, each pixel is represented by 1, but each pixel occupies 1 byte during storage
LBlack and white, each pixel is represented by 1 byte
PPalette mapping, each pixel is represented by 1 byte
RGBRed, green and blue, 3 channels, each pixel is represented by 1 byte
RGBARed, green, blue and transparent, 4 channels, each pixel is represented by 1 byte
CMYKColor isolation mode, 4 channels, each pixel is represented by 1 byte
YCbCrBrightness, blue difference, red difference, 3 channels, each pixel is represented by 1 byte
IEach pixel is represented by a 4-byte integer
FEach pixel is represented by a 4-byte floating-point type

4, Introduction to use

  1. Installing and importing the pilot module

    pip install pillow
    
    from PIL import Image, ImageDraw, ImageGrab
    from PIL import ImageFilter, ImageFont, ImageColor
    
  2. Opening and saving image files

    # Open image file
    im = Image.open(r'testpic.jpg')
    # Image mode
    print(im.mode)
    # Image resolution
    print(im.size)
    # Call the system default image viewing tool to display the image
    im.show()
    # Change RGB mode to L mode
    im_gray = im.convert('L')
    im_gray.show()
    # Image mode
    print(im_gray.mode)
    im_gray.save('new_testpic.jpg')
    
  3. Channel and merge and split

    # Open image file
    im = Image.open(r'testpic.jpg')
    # Split the RGB image into three independent channels
    r, g, b = im.split()
    # Save green channel as file
    g.save(r'testpic_g.jpg')
    # Exchange red and blue channels to get special effects
    im_bgr = Image.merge('RGB', (b, g, r))
    # Save the image after switching channels
    im_bgr.save(r'testpicb_bgr.jpg')
    
  4. Rotate, scale, crop, copy and paste

    # Open image file
    im = Image.open(r'testpic.jpg')
    # Rotate 30 ° counterclockwise to return the new image at the original resolution
    im_30 = im.rotate(30)
    im_30.show()
    print(im_30.size)
    im_30.save(r'testpicnew.jpg')
    # Rotate 30 ° counterclockwise to return to the expanded new image
    im.rotate(30, expand=True).show()
    # Cut 250 * 150 local image
    im_box = im.crop((150, 50, 400, 200))
    # Copying this local image is not a necessary condition for pasting. It is mainly used to demonstrate the copy function
    im_copy = im_box.copy()
    # Paste to the lower right corner of the map
    im.paste(im_copy, (350, 450))
    im.show()
    # Save pasted image
    im.save(r'testpicpaste.jpg')
    
  5. Using filters

    im = Image.open(r'testpic.jpg')
    # Use enhanced filters
    im.filter(ImageFilter.DETAIL).show()
    # Use blur filter
    im.filter(ImageFilter.BLUR).show()
    # Use contour filter
    im.filter(ImageFilter.CONTOUR).show()
    # Using the outline boundary filter
    im.filter(ImageFilter.FIND_EDGES).show()
    
  6. mapping

    im = Image.new("RGB", (800, 300), color=(32, 64, 128))
    draw = ImageDraw.Draw(im)
    draw.line((0, 200, 800, 200), width=2, fill=(255, 255, 255))
    draw.arc([20, 20, 180, 180], 0, 270, fill=(0, 255, 255))
    draw.arc([200, 40, 360, 160], 0, 360, fill=(0, 255, 255))
    draw.pieslice([380, 20, 540, 180], 30, 330, fill='red', outline='white')
    draw.ellipse([380, 20, 540, 180], fill='yellow', outline='white')
    draw.point([660, 100, 670, 100, 680, 100], fill='red')
    draw.rectangle([100, 220, 700, 280], fill=(64, 192, 192), outline='white')
    font = ImageFont.truetype("simfang.ttf", 32)
    draw.text([130, 230], "Life is short, I use it Python", font=font, fill='white')
    im.show()
    
  7. Screenshot

    # Intercept the screen area with the size of 720 * 480
    im = ImageGrab.grab((1200, 600, 1920, 1080))
    im.show()
    # Capture the entire screen
    im = ImageGrab.grab()
    im.show()
    

Tags: Python

Posted on Sat, 04 Sep 2021 21:33:08 -0400 by baby_g