1. Installation
-
Considered to be the official image processing library for python
-
PIL is well suited for image archiving and batch processing of images. You can use PIL to create thumbnails, convert image formats, print images, and more
-
PIL supports many GUI framework interfaces, which can be used for image display
-
The PIL library also supports image size conversion, image rotation, and arbitrary affine transformations
pip install Pillow
2. Open the picture
from PIL import Image im = Image.open("picture.jpg") im.show()
3. Convert the format and save
from PIL import Image im = Image.open("picture.jpg") im.save("result.png")
4. Create thumbnails
from PIL import Image im = Image.open("picture.jpg") im.thumbnail((128, 128)) im.save("result.jpg")
5. Get image properties
-
Gets the source of the image, or None if the image was not read from a file.
from PIL import Image im = Image.open("picture.jpg") print(im.format)
6. Picture information
from PIL import Image im = Image.open("picture.jpg") print(im.info)
7. Palette
-
Returns an instance of the Image Palette class if the image's mode is "P"; otherwise, will be None
from PIL import Image im = Image.open("picture.jpg") print(im.palette)
Eight, drawing board
-
Generates an artboard with the given variables mode and size
from PIL import Image im= Image.new("RGB", (128, 128), "#FF0000") im.show()
9. Picture mode
-
The mode of the image, the common ones are as follows
-
L: 8-bit pixels, black and white
-
P: 9-bit pixels, mapped to any other mode using a palette
-
1: 1-bit pixel, black and white image, stored as 8-bit pixel
-
RGB: 3*8 bit pixels, true color
-
RGBA: 4*8-bit pixels, true color + transparent channel
-
CMYK: 4*8-bit pixels, printing four-color mode or color printing mode
-
YCbCr: 3*8 bit pixels, color video format
-
I: 32-bit integer pixel
-
F: 33-bit floating-point pixels
from PIL import Image im = Image.open("picture.jpg") print(im.mode)
10. Mode conversion
-
Convert the current image to another mode, and return the new image
from PIL import Image im = Image.open("picture.jpg") new_im = im.convert('L') print(new_im.mode) new_im.show()
11. Matrix mode conversion
-
Convert an "RGB" image to an "L" or "RGB" image using a transformation matrix
from PIL import Image im = Image.open("picture.jpg") print(im.mode) matrix = (0.412453,0.357580, 0.180423, 0, 0.212671,0.715160, 0.072169, 0, 0.019334,0.119193, 0.950227, 0 ) new_im = im.convert("L", matrix) print(new_im.mode) new_im.show()
12. Picture size
-
Get the size of the image, calculated according to the number of pixels, and its return value is a two-tuple of width and height
from PIL import Image im = Image.open("picture.jpg") print(im.size)
Thirteen, channel separation
-
Returns a tuple consisting of each channel of the current image
-
Splitting an RGB image will produce three new images
-
Corresponding to each channel red, green and blue images of the original image respectively
from PIL import Image im = Image.open("picture.jpg") r,g,b = im.split() print(r.mode) print(r.size) print(im.size)
Fourteen, copy, cut, paste, merge
from PIL import Image im = Image.open("picture.jpg") # copy im.copy() # cropping im.crop((100, 100, 400, 400)) # Paste one image onto another image im.paste(im.transpose(Image.ROTATE_180),im) # Merge class takes some single channel images and creates a new image Image.merge("RGB", im.split())
15. Geometric Transformation
-
The Image class has resize(), rotate() and transpose(), transform() methods for geometric transformation
from PIL import Image im = Image.open("picture.jpg") # modify size im.resize((128, 128)) # angle rotation im.rotate(45) # Returns a flipped or rotated copy of the current image # 值为:FLIP_LEFT_RIGHT,FLIP_TOP_BOTTOM,ROTATE_90,ROTATE_180,ROTATE_270 im.transpose(Image.ROTATE_90) # Generate a new image with the given dimensions, with the same schema as the original im.transform((200, 200), Image.EXTENT, (0, 0, 300, 300))
16. Advanced Image Processing
-
This can be set quickly using the ImageEnhance object. Contrast, brightness, color balance and sharpness can be adjusted
from PIL import Image from PIL import ImageEnhance im = Image.open("picture.jpg") enh = ImageEnhance.Contrast(im) enh.enhance(1.3).show("30% more contrast")
Seventeen, filter processing
from PIL import Image from PIL import ImageFilter im = Image.open("picture.jpg") # mean filtering im1 = im.filter(ImageFilter.BLUR) # Find contours im2 = im.filter(ImageFilter.CONTOUR) # edge detection im3 = im.filter(ImageFilter.FIND_EDGES) im.show() im1.show() im2.show() im3.show()
18. Set transparency to synthesize two pictures
-
Generate a new image using the given two images and the transparency variable alpha
-
The variable alpha has a value between 0 and 1
-
If the variable alpha is 0.0, returns a copy of the first image. If the variable alpha is 1.0, a copy of the second image will be returned.
from PIL import Image im1 = Image.open("picture1.jpg") im2 = Image.open("picture2.jpg") im = Image.blend(im1, im2, 0.30) im.show()
19. Set the mode to combine two pictures
-
Generate a new image using the given two images and the mask image as transparency
-
The mode of the image can be "1", "L" or "RGBA"
-
All images must have the same dimensions
from PIL import Image im1 = Image.open("picture1.jpg") im2 = Image.open("picture2.jpg") im = Image.composite(im1, im2, 'L') im.show()
20. Draft mode
-
Where speed is more important than quality
-
Allows to convert the image to a given mode and size as much as possible (may not be exactly equal to the given parameters) without reading the file content, which is very effective when generating thumbnails
from PIL import Image im = Image.open("picture.jpg") print(im.size,im.mode) new_im = im.draft("L", (200,200)) print(new_im.size,new_im.mode) new_im.show()
21. Get the channel name
-
Returns a tuple containing the name of each channel
-
For example, for an RGB image it will return ("R", "G", "B")
from PIL import Image im = Image.open("picture.jpg") print(im.getbands())
22. Get the bounding box
-
Calculate the bounding box of the non-zero region of the image
-
The bounding box is a 4-tuple defining left, top, right and bottom pixel coordinates
-
If the image is empty, this method will return empty
from PIL import Image im = Image.open("picture.jpg") print(im.getbbox())
Twenty-three, get the pixel value
from PIL import Image im = Image.open("picture.jpg") print ( im . getdata ( ) )
24. Get the extreme value of the picture
-
returns a 2-tuple
-
Include min and max values in this image
from PIL import Image im = Image.open("picture.jpg") print(im.getextrema())
Twenty-five, specify the pixel value of the position
from PIL import Image im = Image.open("picture.jpg") print(im.getpixel((10,0)))
Twenty-six, get the image histogram
from PIL import Image im = Image.open("picture.jpg") im_histogram = im.histogram() print(im_histogram[0])
Twenty-seven, memory allocation
-
Allocate memory for the image and load it from a file
-
Returns a pixel access object for reading and modifying pixels
from PIL import Image im = Image.open("picture.jpg") pix = im.load() print(pix[0,2])
Twenty-eight, find the specified frame
-
Find the specified frame in the given animation
-
If the search goes beyond the end of the sequence, an EOFError exception is raised
-
When the file is opened, the PIL library is automatically assigned to frame 0
from PIL import Image im_gif = Image.open("loading.gif") print(im_gif.mode) # When it is turned on by default, it is frame 0 im_gif.show() im_gif.seek(1) im_gif.show() im_gif.seek(3) im_gif.show()
29. Get the current frame position
from PIL import Image im_gif = Image.open("loading.gif") print(im_gif.tell()) im_gif.seek(3) print(im_gif.tell())