Python image processing practice
Start with making emoticons: A practical introduction to Python image processing
Imagine this scenario: You want to reduce the cat owner’s beautiful photos to a thumbnail in Moments with one click, automatically add a cute watermark, draw a playful outline for the cat’s face, or even generate an exclusive static holiday card. These seemingly “designer” needs can be easily accomplished with Python. And the library behind it that can turn you into an "image magician" with just a few lines of code is Pillow.
In the actual project of Daoman Python AI, Pillow has always been regarded as the "Swiss Army Knife-level entry library" for data preprocessing and visual tool chains. Play with it thoroughly first, and then try hard-core frameworks such as OpenCV and TensorFlow Lite. It will be much smoother to get started.
1. Two-minute literacy: the "smallest parts" of images
Before typing code, there are two core concepts that must be brushed up quickly. Although the principle is simple, anyone who has stepped on the coordinate pit knows their importance.
1.1 Color model (RGB/RGBA)
The computer screen relies on the superposition of three colors of red, green, and blue to display the colorful world. This is the RGB color model.
- The intensity of each color light is represented by an integer between 0~255 (256 levels of grayscale). The larger the value, the brighter the color.
- RGBA has an additional Alpha channel on this basis, which also takes 0~255, representing transparency (0 is completely transparent, 255 is completely opaque). This channel is crucial when compositing watermarks and stickers.
Quick Check Card
1.2 pixels
Continuously enlarge a photo or screenshot to the extreme, and what you will see are densely packed small blocks of color - these are pixels, the smallest editable unit of an image. A 1920×1080 picture has a total of more than 2 million such color blocks. To put it bluntly, all operations of Pillow are helping us process these "small grids" in batches.
2. Get started quickly with Pillow: from installation to "fancy photo editing"
Pillow is a modern replica of PIL (Python Imaging Library). It has good compatibility and a particularly intuitive API design. It is currently the first choice for Python 3.x to process images.
Installation in one step
2.1 Basic five moves: Open → View information → Crop → Zoom → Transfer
The core module isPIL.Image. The following process is exactly the same as when you use PS or mobile photo editing apps. It is recommended to match your own pictures (for example, name itcat_hero.jpg) run together.
2.2 Advanced gameplay: filter + sticker synthesis
Pillow has built-in a lot of ready-to-use filters, plus a flexible paste function, allowing you to easily create "cat emoticons".
Sticker synthesis: add a heart to the cat owner
Tips Paste function
paste()The third parameter ofmaskIt is a very powerful design: you can use it to precisely control "only the sticker itself, not the background". Particularly friendly to translucent edges.
3. Static drawing: "draw" a greeting card with code
Pillow can not only retouch images, but also create from scratch on a blank canvas.PIL.ImageDrawThe module allows you to use code to draw geometric shapes, write text, easily handle verification codes, holiday greeting cards, or batch generate posters with dates.
NOTE Pillow 9.2.0 and later versions are recommended
textbbox()To dynamically obtain the width and height of the text area. oldtextsize()The method has been marked as obsolete and is recommended for gradual replacement.
Summary and next steps
Shorthand for practical key points in this article
- Coordinate system: The origin is in the upper left corner, x increases to the right, and y increases downward.
- Core module:
Image(read/write/basic operations),ImageFilter(filter),ImageDraw(drawing),ImageFont(text rendering). - Transparent layer: To retain the transparency effect, remember to convert the image to
RGBA;pass when pastingmaskParameter controls the visible area.
What can you learn next?
- Zero threshold advancement: Use Pillow to batch process photos (such as converting to grayscale with one click, adding watermarks in batches).
- Enter the hard-core vision field: If you want to do face recognition, target detection, real-time video processing, just get on OpenCV-Python.
- Deep Learning Preprocessing: Pillow is a commonly used image preprocessing tool in official examples of TensorFlow/Keras and PyTorch. Mastering it will allow you to enter the door of deep learning image tasks more smoothly.
Now, you might as well open the terminal, pick your favorite photo, and use Pillow to add a bit of programmer romance to it!

