Python with Pillow (PIL) for simple image manipulation methods
Colors and RGBA values
Computers usually represent images as RGB values, or with an alpha value (translucency, transparency), called RGBA values. In Pillow, RGBA values are represented as a tuple of four integers, R, G, B, and A. The integers range from 0 to 255. All 0s in RGB represent black, and all 255s represent black. It can be guessed that (255, 0, 0, 255) represents red color, because the R component is the largest, and the G and B components are 0, so it is rendered as red. But when the alpha value is 0, no matter what the color is, the color is not visible and can be understood as transparent.
from PIL import ImageColor print(('red', 'RGBA')) # Can also be viewed as RBG only print(('black', 'RGB'))
(255, 0, 0, 255) (0, 0, 0)
Coordinate representation of images
The upper left corner of the image is the coordinate origin (0, 0), which is not quite the same as the usual coordinate system in math. A coordinate system defined this way means that the X-axis grows from left to right, while the Y-axis grows from top to bottom.
How can I represent a rectangular area in Pillow using the coordinate system defined above? Many functions or methods require a rectangular tuple parameter. The tuple parameter contains four values representing the distance of each of the four sides of the rectangle from the X or Y axis. The order is (left, top, right, bottom). The right and bottom coordinates are slightly special, representing up to but not including. They can be interpreted as intervals like [Left, Right) and [Top, Bottom) which are left-closed and right-open. For example, (3, 2, 8, 9) represents a rectangular region with horizontal coordinates in the range [3, 7] and vertical coordinates in the range [2, 8].
Manipulating Images with Pillow
After understanding some basics, you can get started. First start with reading images, many image processing libraries (such as opencv) read images as imread(). open method is used in Pillow.
from PIL import Image im_path = r'F:\Jupyter Notebook\csv_time_datetime_PIL\' im = (im_path) width, height = # Width and height print(, width, height) # format, and a detailed description of the format print(, im.format_description) (r'C:\Users\Administrator\Desktop\rabbit_copy.jpg') ()
(1920, 1080) 1920 1080 JPEG JPEG (ISO 10918)
Returns a tuple, respectively width and height. show() method will call the system default image viewing software, open and display. The format of the image can be viewed. save() can save the processed image, if unprocessed, the saved image takes up space (number of bytes) is also generally not the same as the original image, may have been compressed.
New Image
Pillow can also create a new blank image, the first parameter is mode, that is, the color space mode, the second parameter specifies the resolution of the image (width x height), the third parameter is the color.
You can directly fill in the name of a common color. For example, 'red'
It can also be filled with a hexadecimal representation of the color, such as #FF0000 for red.
It is also possible to pass in tuples such as (255, 0, 0, 255) or (255, 0, 0) for the color red.
# Usually just use RGB mode newIm= ('RGB', (100, 100), 'red') (r'C:\Users\Administrator\Desktop\') # You can also use RGBA mode. Check the documentation for other modes. blcakIm = ('RGB',(200, 100), 'red') (r'C:\Users\Administrator\Desktop\') # Hexadecimal colors blcakIm = ('RGBA',(200, 100), '#FF0000') (r'C:\Users\Administrator\Desktop\') # Pass in RGBA values or RGB values in tuple form # In RGB mode, the fourth parameter is invalidated and defaults to 255, in RGBA mode, it is also possible to pass in only the first three values and the A value defaults to 255 blcakIm = ('RGB',(200, 100), (255, 255, 0, 120)) (r'C:\Users\Administrator\Desktop\')
Cropping images
Image has a crop() method that takes a tuple of rectangular regions (mentioned above). It returns a new Image object, which is the cropped image and has no effect on the original image.
im = (im_path) cropedIm = ((700, 100, 1200, 1000)) (r'C:\Users\Administrator\Desktop\')
Take a look at the original and cropped images.
Copying and pasting an image to another image
The Image's copy function, as its name suggests, produces a copy of the original image, and any operations on this copy will not affect the original image. paste() method is used to paste (overlay) one image on top of another. Whoever calls it will make changes directly on the Image object.
im = (im_path) cropedIm = ((700, 100, 1200, 1000)) (cropedIm, (0, 0)) () (r'C:\Users\Administrator\Desktop\')
() Displaying the image reveals that at this point im (i.e., the original image) has been altered.
This is if the information of the original image will be used again afterward, as the information is changed it is troublesome. So it is better to use copy() to make a copy before paste, and the operation in this copy will not affect the information of the original picture. Although the information of the original image has been changed in the program, but due to the other file name used when saving the file, it is equivalent to the change did not take effect, so the original image is still unchanged when you view it.
im = (im_path) cropedIm = ((700, 100, 1200, 1000)) copyIm = () (cropedIm, (0, 0)) () (r'C:\Users\Administrator\Desktop\')
This time, when you look at the original image again, it hasn't changed. This ensures that when im is used again later, the information in it is still original. Let's look at an interesting example.
im = (im_path) cropedIm = ((700, 100, 1200, 1000)) crop_width, crop_height = width, height = copyIm = () for left in range(0, width, crop_width): for top in range(0, height, crop_height): (cropedIm, (left, top)) (r'C:\Users\Administrator\Desktop\')
Take the width and height of the cropped image at intervals and keep pasting in copies within the loop, it's kind of like taking a photo ID.
Resizing images
The resize method returns a new Image object of the specified width and height, accepting a tuple containing the width and height as arguments. The value of the width and height is an integer.
im = (im_path) width, height = resizedIm = ((width, height+(1920-1080))) (r'C:\Users\Administrator\Desktop\')
The rabbit is thin and you can see that resize is not scaled equally.
Rotate and flip images
rotate() returns the rotated Image object, leaving the original image unchanged. Counterclockwise rotation.
im = (im_path) (90).save(r'C:\Users\Administrator\Desktop\') (270).save(r'C:\Users\Administrator\Desktop\') (180).save(r'C:\Users\Administrator\Desktop\') (20).save(r'C:\Users\Administrator\Desktop\') (20, expand=True).save(r'C:\Users\Administrator\Desktop\rotate20_expand.jpg')
From top to bottom, they are rotated 90 °, 180 °, 270 °, ordinary 20 °, plus the parameter expand = True rotated 20 °. expand enlarges the image size (into 2174x1672), so that the corners of the image is not cropped (four corners just to the edge of the image). Then look at the rotation of 90 °, 270 ° when the image is cropped, but the following view of the image width and height, is the same as the original image, can not understand.
im90 = (r'C:\Users\Administrator\Desktop\') im270 = (r'C:\Users\Administrator\Desktop\') # The width and height information didn't change print(, )
(1920, 1080) (1920, 1080)
Mirror flip of the image. transpose() function can be implemented, you must pass Image.FLIP_LEFT_RIGHT or Image.FLIP_TOP_BOTTOM, the first is a horizontal flip, the second is a vertical flip.
im = (im_path) (Image.FLIP_LEFT_RIGHT).save(r'C:\Users\Administrator\Desktop\transepose_lr.jpg') (Image.FLIP_TOP_BOTTOM).save(r'C:\Users\Administrator\Desktop\transepose_tb.jpg')
Horizontal flip is not visible, the original image is horizontally symmetrical...
A vertical flip would be obvious...
Image Filtering
Pillow uses ImageFilter to simply do the image blurring, edge enhancement, sharpening, smoothing and other common operations.
from PIL import Image, ImageFilter im = (im_path) # Gaussian blur ().save(r'C:\Users\Administrator\Desktop\') # Normal blur ().save(r'C:\Users\Administrator\Desktop\') # Edge enhancement (ImageFilter.EDGE_ENHANCE).save(r'C:\Users\Administrator\Desktop\EDGE_ENHANCE.jpg') # Find the edge (ImageFilter.FIND_EDGES).save(r'C:\Users\Administrator\Desktop\FIND_EDGES.jpg') # Relief ().save(r'C:\Users\Administrator\Desktop\') # Profile ().save(r'C:\Users\Administrator\Desktop\') # Sharpening ().save(r'C:\Users\Administrator\Desktop\') # Smooth ().save(r'C:\Users\Administrator\Desktop\') # Details ().save(r'C:\Users\Administrator\Desktop\')
In addition, if you want to pattern, text drawing, you can use ImageDraw. Pillow has other powerful features, not to mention.
Actually, Pillow is just a basic image processing library. If you don't dive into image processing, it's good enough. Professionals using opencv is a better choice. use import cv2 in Python to get started!
Above this Python with Pillow (PIL) for simple image manipulation method is all I have shared with you, I hope to give you a reference, and I hope you support me more.