ImageEnhance Module
The ImageEnhance module contains a number of classes that can be used for image enhancement. Each class takes a single argument, the image, and returns an enhancer object with an enhance(factor) method. A factor of 1.0 always returns a copy of the original image.
| Class | Factor = 0 | Factor = 1.0 | Factor = 2.0+ |
|---|---|---|---|
ImageEnhance.Contrast(image) | Solid gray image | Original | Higher contrast |
ImageEnhance.Brightness(image) | Black image | Original | Brighter |
ImageEnhance.Color(image) | Grayscale | Original | More vibrant colour |
ImageEnhance.Sharpness(image) | Blurred | Original | Sharpened |
ImageFont Module
The ImageFont module defines a class with the same name. Instances of this class store bitmap fonts, used with ImageDraw.text().
| Function | Description |
|---|---|
ImageFont.truetype(font, size, index=0, encoding="") | Load a TrueType or OpenType font from a file path or URL. Returns an FreeTypeFont object. |
ImageFont.load(filename) | Load a BDF or PCF bitmap font. Limited to ASCII characters. |
ImageFont.load_default(size=None) | Load the built-in default bitmap font. If size is given, loads the default TrueType font at that size (Pillow 10.1+). |
FreeTypeFont.getbbox(text) | Returns (left, top, right, bottom) bounding box of the text. |
FreeTypeFont.getlength(text) | Returns the rendered length of the text in pixels. |
FreeTypeFont.getmetrics() | Returns (ascent, descent). |
FreeTypeFont.font_variant(size, index, encoding, font_path) | Create a new FreeTypeFont at a different size, useful for caching font objects. |
ImageGrab Module
The ImageGrab module can be used to copy the contents of the screen or the clipboard to a PIL image memory. Only supported on Windows, macOS, and Linux (with Pillow[screenshot] extra).
| Function | Description |
|---|---|
ImageGrab.grab(bbox=None, include_layered_windows=False, all_screens=False, xdisplay=None) | Take a screenshot. bbox is (left, upper, right, lower) to capture region. Default is full screen. |
ImageGrab.grabclipboard() | Grab the current contents of the clipboard as an image. Returns None if clipboard contains non-image data. |
ImageChops Module (Channel Operations)
The ImageChops module contains a number of arithmetical image operations, called channel operations ("chops"). Operations are applied to every pixel, for all channels.
| Function | Formula | Description |
|---|---|---|
add(image1, image2, scale=1.0, offset=0) | (im1 + im2) / scale + offset | Add two images. Clip to 0–255. |
subtract(image1, image2, scale=1.0, offset=0) | (im1 - im2) / scale + offset | Subtract two images. |
multiply(image1, image2) | im1 * im2 / MAX | Multiply two images. Making darker. |
screen(image1, image2) | MAX - (MAX-im1)*(MAX-im2)/MAX | Screen blend. Opposite of multiply — making lighter. |
difference(image1, image2) | |im1 - im2| | Absolute difference. Useful for detecting changes between frames. |
add_modulo(image1, image2) | (im1 + im2) % MAX | Add modulo MAX (wraps around, no clipping). |
subtract_modulo(image1, image2) | (im1 - im2) % MAX | Subtract modulo MAX. |
invert(image) | MAX - im | Invert the image. |
lighter(image1, image2) | max(im1, im2) | Return brighter pixels from both images. |
darker(image1, image2) | min(im1, im2) | Return darker pixels from both images. |
logical_and(image1, image2) | AND | Logical AND on pixel data. |
logical_or(image1, image2) | OR | Logical OR on pixel data. |
logical_xor(image1, image2) | XOR | Logical XOR on pixel data. |
constant(image, value) | — | Return a copy filled with the given pixel value. |
duplicate(image) | — | Return a copy of the image. |
offset(image, xoffset, yoffset=None) | — | Return a copy with pixels shifted by offset. Wraps around. |
composite(image1, image2, mask) | — | Composite two images using a mask. |
hard_light(image1, image2) | — | Hard light blend. Strong contrast effect. |
soft_light(image1, image2) | — | Soft light blend. Subtle lightening/darkening. |
overlay(image1, image2) | — | Overlay blend mode. Multiply for darks, screen for lights. |
ImageStat Module
The ImageStat module calculates global statistics for an image, or for a region of an image. Each statistic is returned as a list of values, one per band.
| Attribute | Type | Description |
|---|---|---|
stat.extrema | list of (min, max) tuples | Min and max value per band. |
stat.count | list of int | Number of pixels per band. |
stat.sum | list of float | Sum of all pixel values per band. |
stat.sum2 | list of float | Sum of squares per band. |
stat.mean | list of float | Average pixel value per band. |
stat.median | list of float | Median pixel value per band. |
stat.rms | list of float | Root mean square per band. |
stat.var | list of float | Variance per band. |
stat.stddev | list of float | Standard deviation per band. |
ImageMath Module
The ImageMath module can be used to evaluate "image expressions". The module provides two evaluation functions — choose based on whether you trust the input expressions.
| Function | Description |
|---|---|
ImageMath.lambda_eval(expression, environment={}) | Evaluate expression using a lambda. Safe for untrusted input. Expression is a callable taking band values. |
ImageMath.unsafe_eval(expression, **environment) | Evaluate expression string. Only use with trusted input. Supports operators: +, -, *, /, **, &, |, ^, ~, and functions like min(), max(), abs(), float(), int(). |
ImageSequence Module
The ImageSequence module contains a wrapper class that lets you iterate over the frames of an image sequence.
ImageColor Module
The ImageColor module converts CSS3-style colour definitions to RGB tuples for use in Pillow drawing functions.
| Function | Description |
|---|---|
ImageColor.getrgb(color) | Convert a colour string to an RGB or RGBA tuple. Supports named colours, hex (#rgb, #rrggbb, #aarrggbb), rgb(), rgba(), hsl(), hsla(). |
ImageColor.getcolor(color, mode) | Same as getrgb() but converts to the given image mode. Useful for passing to Image.new(). |