Pillow Tutorial
Pillow is the primary library for image processing in Python.
Pillow is a friendly fork of the Python Imaging Library (PIL).
Since the original PIL is no longer maintained, Pillow has taken over its role and continues active development.
Pillow provides extensive file format support, efficient internal representation, and powerful image processing capabilities.
Who is this tutorial for?
This tutorial is suitable for developers with a foundation in Python.
If you don't know Python yet, you can first learnPython Tutorial。
Main Features
- Supports multiple image formats (JPEG, PNG, GIF, BMP, TIFF, etc.)
- Basic image processing operations (rotate, resize, crop, etc.)
- Image filtering and enhancement
- Supports image drawing
- Color space conversion
Comparison with other libraries
- Compared to OpenCV: Pillow focuses more on image processing rather than computer vision, with a simpler API.
- Compared to Matplotlib: Pillow focuses more on image processing rather than data visualization.
- Compared to scikit-image: Pillow is more basic and easier to get started with, suitable for general image processing tasks.
First Pillow Example
After installing Pillow, still use import PIL orfrom PIL import ...to import the module.
The following example shows basic information about an image:
Example
from PIL import Image
# Open an image
img = Image.open("example.jpeg")
# Display the image
img.show()
# Get image information
print(Image format:, img.format) # JPEG, PNG, etc.
print(Image size:, img.size) # (width, height)
print(Image mode:, img.mode) # RGB, L, etc.
# Save as PNG format
img.save("example.png")
# Open an image
img = Image.open("example.jpeg")
# Display the image
img.show()
# Get image information
print(Image format:, img.format) # JPEG, PNG, etc.
print(Image size:, img.size) # (width, height)
print(Image mode:, img.mode) # RGB, L, etc.
# Save as PNG format
img.save("example.png")
Output:
图片格式: JPEG 图片大小: (987, 987) 图片模式: RGB
After running, the image is also saved asexample.png。