Getting Started with Python Image Processing: Get Started Quickly with Pillow
1. Pillow Introduction
In daily development, whether it is generating website thumbnails, making random verification codes, or cropping image sizes in batches, there is no need to resort to complex Photoshop scripts or third-party APIs - Python's Pillow library can easily do it.
Pillow is the modern branch of the old Python Imaging Library (PIL): while retaining PIL's simple API, it is fully compatible with Python 3, and has added new format support, performance optimization and other features. It is currently the most popular lightweight image processing tool in the Python ecosystem.
Core Highlights
- Supports over 40 image formats (JPEG, PNG, BMP, GIF, TIFF, WebP, etc.)
- Covers common image processing needs from basic to advanced
- The code logic is clear and novice-friendly
- Active community, stable version iterations
2. Quick installation
Standard pip installation
Anaconda/Miniconda environment
When installing as a normal user on Linux/macOS, it is recommended to add
--userParameters to avoid modifying the system environment:
3. Basic image operations
3.1 Open, view and save
This is the core first step of Pillow, and the code is almost as intuitive as "opening Notepad":
3.2 Resizing
Pillow provides two common methods:
thumbnail(): Reduce proportionally and modify the image in place to ensure that the image does not exceed the specified size.resize(): Force stretching or scaling, returning a new image with fully controllable size.
💡 Note:
thumbnail()will directly modify the original image, whileresize()Return a brand new image. If you need to use the original size later, please make a copy first.
3.3 Rotation and flipping
Rotation and flipping are both out-of-place operations that return a new image:
4. Apply built-in filters with one click
Pillow has more than a dozen commonly used filters built-in, eliminating the need to write complex image processing algorithms by hand:
5. Simple image drawing
combineImageDrawandImageFont, you can easily generate verification codes, text watermarks, simple charts, etc.
5.1 Basic graphics drawing
5.2 Useful gadget: generate 4-digit verification code
The verification code logic of many websites can be implemented using this simplified version:
6. Advanced small functions
6.1 Image synthesis with transparency
Overlay a PNG watermark with a transparent channel onto a photo, often used to add branding in batches:
7. Quick FAQ
-
System font loading failed Replace the font path with the real absolute path of the corresponding system, or use it directly
ImageFont.load_default()Loads the default bitmap font. -
An error occurs when RGBA images are saved directly as JPEG The JPEG format does not support transparency and needs to be converted to RGB mode first:
-
Memory overflow when processing large images priority use
thumbnail()Reduce the image proportionally to avoid direct calls to oversized imagesresize()。
8. Learning resources
- 📖 Official documentation: https://pillow.readthedocs.io/
- 📂 Official sample library: https://github.com/python-pillow/Pillow/tree/main/Examples
Through this tutorial, you have mastered 80% of Pillow's core usage scenarios, and the remaining details can be explored on demand according to the official documentation!

