ImageOps Module
The ImageOps module contains a number of ready-made image processing operations. For the most part, this module operates on L and RGB images.
autocontrast()
Maximize (normalize) image contrast. Calculates the histogram for the image, removes a cutoff percent of the lightest and darkest pixels from the histogram, and then remap the image so that the darkest pixel becomes black and the lightest becomes white.
| Parameter | Type | Description |
|---|---|---|
image | Image | Input image (L or RGB). |
cutoff | float | Percent to cut from histogram edges. Default: 0. Can be 2-tuple: (low, high). |
ignore | int | sequence | Background pixel value(s) to ignore during computation. |
mask | Image | None | Mask restricting the histogram calculation region. |
preserve_tone | bool | If True, the RGB channels will be individually autocontrasted (deprecated). |
colorize()
Colorize a grayscale image by mapping each grayscale level to a colour between two (or three) colour endpoints.
| Parameter | Description |
|---|---|
image | Grayscale (L mode) image. |
black | Colour (name, hex, or tuple) to use for black pixels. |
white | Colour to use for white pixels. |
mid | Optional midpoint colour. Default: None. |
blackpoint | Grayscale level (0-255) that maps to black. Default: 0. |
whitepoint | Grayscale level (0-255) that maps to white. Default: 255. |
midpoint | Grayscale level that maps to mid. Default: 127. |
equalize()
Equalize the image histogram. Applies a non-linear mapping to the input image, in order to create a uniform distribution of grayscale values in the output image. Very effective on low-contrast images.
expand()
Add a border to the image. The border parameter controls the width — it can be a single integer (same on all sides) or a 4-tuple (left, top, right, bottom).
fit() and pad()
fit() returns a sized and cropped version of the image, cropped to the requested aspect ratio and size. pad() returns a sized and padded version that never crops.
| Method | Behaviour |
|---|---|
fit(image, size, method, bleed, centering) | Crops to fill the target size. Preserves aspect ratio. May remove edges. |
pad(image, size, method, color, centering) | Pads with color to reach target size. Never crops. Adds letterbox/pillarbox bars. |
grayscale() and invert()
posterize() and solarize()
posterize() reduces the number of bits per channel. solarize() inverts all values above a threshold (mimics exposing film to light during development).
exif_transpose()
If an image has an Exif Orientation tag, returns a new image that is transposed accordingly. Often the most important function in the module for processing user-uploaded photos from mobile devices.
All ImageOps Functions
| Function | Description |
|---|---|
autocontrast(image, cutoff, ignore, mask, preserve_tone) | Normalize contrast using histogram. |
colorize(image, black, white, mid, blackpoint, whitepoint, midpoint) | Colorize grayscale image. |
crop(image, border) | Remove border pixels from image. |
scale(image, factor, resample=BICUBIC) | Scale image by a factor. 2.0 = double size. |
degenerate(image) | Return a degenerate image, equivalent to a greyscale image viewed in color. |
equalize(image, mask) | Equalize image histogram. |
expand(image, border, fill) | Add border padding. |
fit(image, size, method, bleed, centering) | Crop-scale to target size. |
flip(image) | Flip image top to bottom. |
grayscale(image) | Convert to grayscale (keeps mode). |
invert(image) | Invert (negate) image. |
mirror(image) | Flip left to right. |
pad(image, size, method, color, centering) | Pad image to target size. |
posterize(image, bits) | Reduce colours per channel. |
solarize(image, threshold) | Invert values above threshold. |
exif_transpose(image, in_place=False) | Auto-rotate based on EXIF orientation. |
contain(image, size, method=BICUBIC) | Scale image to fit within size without cropping. |
cover(image, size) | Scale image to cover size entirely (may crop). |