Pillow (formerly known as PIL, Python Imaging Library) is an open source Python image processing library that provides extensive file format support, powerful image processing capabilities, and an easy-to-use API. Pillow can process almost all types of image files and can perform complex image operations such as rotation, scaling, color conversion, etc.
The main features of Pillow
- Extensive file format support: Supports opening, saving and operating multiple image file formats.
- Image processing capability: Provides functions such as image filtering, color conversion, geometric transformation, etc.
- Easy to use API: Provides a simple and intuitive API to process images.
- Image filtering: Including a variety of filter effects such as blur, sharpening, edge detection, etc.
- Image drawing: You can draw text, lines, shapes, etc. on images.
Commonly used Pillow functions and their parameters
()
Open the image file and return aImage
Object.
-
filename
: The path to the image file.
()
Create a new image.
-
mode
: Image mode, such as'RGB'
、'L'
wait. -
size
: The size of the image, format(width, height)
。()
Save the image to the file.
-
filename
: Saved file path. -
format
: File format.
()
Resize the image.
-
size
: New image size. -
resample
: Resampling filter.
()
Crop the image.
-
bbox
: The bounding box of the clip area.
()
Rotate the image.
-
angle
: Rotation angle. -
resample
: Resampling filter.
()
Convert the image.
-
method
: Conversion method, such asImage.FLIP_LEFT_RIGHT
。
()
Apply filter effects.
-
filter
: Filter object.
()
Draw on the image.
-
image
: The image object to be drawn.
()
Load TrueType or OpenType font file.
-
font
: The path to the font file. -
size
: Font size. Example
Here is an example of using Pillow for image processing:
from PIL import Image, ImageDraw, ImageFont # Open the imageimage = ('') # Create a new imagenew_image = ('RGB', (200, 200), 'white') # Paste the original image onto the new imagenew_image.paste(image, (50, 50)) # Rotate the imagerotated_image = new_image.rotate(45, expand=True) # Draw textdraw = (rotated_image) font = ('', 15) ((10, 10), 'Hello, Pillow!', font=font, fill='black') # Save the imagerotated_image.save('')
In this example, we first open an image file and then create a new white image. We pasted the original image onto the new image and rotated the new image. Finally, we draw the text on the image and save the final image.
This is the end of this article about the detailed explanation of the Python Pillow image processing library. For more related content in the Python image processing library, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!