Documentation Menu

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.

Import
from PIL import Image, ImageOps

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.

ParameterTypeDescription
imageImageInput image (L or RGB).
cutofffloatPercent to cut from histogram edges. Default: 0. Can be 2-tuple: (low, high).
ignoreint | sequenceBackground pixel value(s) to ignore during computation.
maskImage | NoneMask restricting the histogram calculation region.
preserve_toneboolIf True, the RGB channels will be individually autocontrasted (deprecated).
autocontrast Example
img = Image.open("dull_photo.jpg") enhanced = ImageOps.autocontrast(img, cutoff=2) enhanced.save("enhanced.jpg")

colorize()

Colorize a grayscale image by mapping each grayscale level to a colour between two (or three) colour endpoints.

ParameterDescription
imageGrayscale (L mode) image.
blackColour (name, hex, or tuple) to use for black pixels.
whiteColour to use for white pixels.
midOptional midpoint colour. Default: None.
blackpointGrayscale level (0-255) that maps to black. Default: 0.
whitepointGrayscale level (0-255) that maps to white. Default: 255.
midpointGrayscale level that maps to mid. Default: 127.
colorize Example
gray = Image.open("photo.jpg").convert("L") colorized = ImageOps.colorize(gray, black="blue", white="yellow") colorized.save("colorized.jpg")

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.

equalize Example
img = Image.open("foggy_photo.jpg") equalized = ImageOps.equalize(img) equalized.save("equalized.jpg")

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).

expand Example
# Add 20px white border on all sides bordered = ImageOps.expand(img, border=20, fill="white") # Custom border per side bordered = ImageOps.expand(img, border=(10,20,10,20), fill="black")

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.

MethodBehaviour
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.
fit() and pad() Examples
# Crop-fit to square (good for profile photos) square = ImageOps.fit(img, (400, 400), method=Image.Resampling.LANCZOS) # Pad to 16:9 with black bars widescreen = ImageOps.pad(img, (1920, 1080), color="black")

grayscale() and invert()

grayscale & invert
# Convert to grayscale (keeps RGB mode) gray = ImageOps.grayscale(img) # Invert: makes darks light and lights dark inverted = ImageOps.invert(img) # Flip / Mirror flipped = ImageOps.flip(img) # top-bottom mirrored = ImageOps.mirror(img) # left-right

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).

posterize & solarize
# posterize: 1-8 bits per channel. 1 = 2 colors, 8 = no change poster = ImageOps.posterize(img, bits=2) # solarize: invert pixels > threshold (0-255) solar = ImageOps.solarize(img, threshold=128)

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.

exif_transpose Example
from PIL import Image, ImageOps img = Image.open("camera_photo.jpg") img = ImageOps.exif_transpose(img) img.save("corrected.jpg")

All ImageOps Functions

FunctionDescription
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).