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 should read this tutorial?
This tutorial is suitable for developers with a Python foundation.
If you are not yet familiar with Python, you can first learn Python Tutorial。
Main Features
- Supports multiple image formats (JPEG, PNG, GIF, BMP, TIFF, etc.)
- Basic image processing operations (rotation, scaling, cropping, etc.)
- Image filtering and enhancement
- Supports image drawing
- Color space conversion
Comparison with Other Libraries
- Compared with OpenCV: Pillow focuses more on image processing than computer vision, with a simpler API
- Compared with Matplotlib: Pillow focuses more on image processing than data visualization
- Compared with scikit-image: Pillow is more basic and easier to get started with, suitable for general image processing tasks
First Pillow Example
After installing Pillow, you still use import PIL or from PIL import ... to import modules.
The following example shows the basic information of an image:
Example
from
PIL
import
Image
Open an image
img
=
Image.
open
(
"example.jpeg"
)
Displaying an Image
img.
show
(
)
Get image information
(
"Image format:"
,
img.
format
)
JPEG, PNG, etc.
(
"Image size:"
,
img.
size
)
(width, height)
(
"Image mode:"
,
img.
mode
)
RGB, L, etc.
Save in PNG format
img.
save
(
"example.png"
)
Running result:
图片格式: JPEG
图片大小: (987, 987)
图片模式: RGB
After running, the image will also be saved as example.png.