Concepts
The Python Imaging Library handles raster images — rectangles of pixel data.
Bands
An image can consist of one or more bands of data. Pillow allows you to store several bands in a single image, provided they all have the same dimensions and depth. For example, a PNG image might have 'R', 'G', 'B', and 'A' bands.
Modes
The mode of an image defines the type and depth of a pixel. Each pixel uses the full range of the bit depth.
| Mode | Description | Bands |
|---|---|---|
1 | 1-bit pixels, black and white. Stored with one pixel per byte. | 1 |
L | 8-bit pixels, grayscale (Luminance). | 1 |
P | 8-bit pixels, mapped to any other mode using a colour palette. | 1 |
RGB | 3×8-bit pixels, true colour. | 3 |
RGBA | 4×8-bit pixels, true colour with transparency mask. | 4 |
CMYK | 4×8-bit pixels, colour separation. Used in print workflows. | 4 |
YCbCr | 3×8-bit pixels, colour video format (JPEG standard). | 3 |
LAB | 3×8-bit pixels, the L*a*b color space. | 3 |
HSV | 3×8-bit pixels, Hue (0-255 scaled 0-360°), Saturation, Value. | 3 |
I | 32-bit signed integer pixels. | 1 |
F | 32-bit floating point pixels. | 1 |
LA | L with alpha. | 2 |
PA | P with alpha. | 2 |
RGBa | RGB with pre-multiplied alpha. | 4 |
La | L with pre-multiplied alpha. | 2 |
I;16 | 16-bit unsigned integer pixels. | 1 |
I;16B | 16-bit big-endian unsigned integer pixels. | 1 |
Coordinate System
Pillow uses a Cartesian coordinate system with the origin (0,0) in the upper left corner. Note that coordinates refer to pixel corners, not pixel centers. So the bounding box for a 100×100 pixel image is: (0, 0, 100, 100).
| Concept | Value |
|---|---|
| Origin | Top-left corner (0, 0) |
| X-axis | Increases rightward (width) |
| Y-axis | Increases downward (height) |
| Bounding box | (left, upper, right, lower) — exclusive on right and lower |
Palette Mode
Palette (P mode) images use a colour lookup table. Each pixel value is an index into this table. This is used by GIF, which is limited to 256 unique colours per frame.
The Info Attribute
The im.info dictionary is populated by the file decoder with format-specific metadata. Common entries include DPI, comments, and EXIF data. This dictionary is not automatically written on save() — you must explicitly pass parameters to the save method.
Filters
The ImageFilter module defines a set of predefined image filters and a generic kernel filter. Filters are applied using im.filter().
| Filter | Category | Description |
|---|---|---|
BLUR | Blur | Box blur. |
GaussianBlur(radius) | Blur | Gaussian bell-curve blur. Default radius=2. |
BoxBlur(radius) | Blur | Each output pixel is the average of the square neighbourhood. |
CONTOUR | Edge | Find image contours. |
DETAIL | Sharpen | Enhance detail. |
EDGE_ENHANCE | Edge | Edge enhancement filter. |
EDGE_ENHANCE_MORE | Edge | More aggressive edge enhancement. |
EMBOSS | Effect | Emboss image. |
FIND_EDGES | Edge | Find sharp edges. |
SHARPEN | Sharpen | Sharpen image. |
SMOOTH | Smooth | Smooth image. |
SMOOTH_MORE | Smooth | More aggressive smoothing. |
UnsharpMask(radius, percent, threshold) | Sharpen | Unsharp mask. radius=2, percent=150, threshold=3. |
Kernel(size, kernel) | Custom | Create custom convolution kernel. |
RankFilter(size, rank) | Rank | Picks rank-th minimum pixel in each neighbourhood. |
MedianFilter(size) | Rank | Picks median pixel — good for "salt and pepper" noise. |
MinFilter(size) | Rank | Picks minimum (darkest) pixel in neighbourhood. |
MaxFilter(size) | Rank | Picks maximum (brightest) pixel in neighbourhood. |