ImageOps Module#

The ImageOps module contains a number of ‘ready-made’ image processing operations. This module is somewhat experimental, and most operators only work on L and RGB images.

New in version 1.1.3.

PIL.ImageOps.autocontrast(image, cutoff=0, ignore=None, mask=None, preserve_tone=False)[source]#

Maximize (normalize) image contrast. This function calculates a histogram of the input image (or mask region), removes cutoff percent of the lightest and darkest pixels from the histogram, and remaps the image so that the darkest pixel becomes black (0), and the lightest becomes white (255).

Parameters:
  • image – The image to process.

  • cutoff – The percent to cut off from the histogram on the low and high ends. Either a tuple of (low, high), or a single number for both.

  • ignore – The background pixel value (use None for no background).

  • mask – Histogram used in contrast operation is computed using pixels within the mask. If no mask is given the entire image is used for histogram computation.

  • preserve_tone

    Preserve image tone in Photoshop-like style autocontrast.

    New in version 8.2.0.

Returns:

An image.

PIL.ImageOps.colorize(image, black, white, mid=None, blackpoint=0, whitepoint=255, midpoint=127)[source]#

Colorize grayscale image. This function calculates a color wedge which maps all black pixels in the source image to the first color and all white pixels to the second color. If mid is specified, it uses three-color mapping. The black and white arguments should be RGB tuples or color names; optionally you can use three-color mapping by also specifying mid. Mapping positions for any of the colors can be specified (e.g. blackpoint), where these parameters are the integer value corresponding to where the corresponding color should be mapped. These parameters must have logical order, such that blackpoint <= midpoint <= whitepoint (if mid is specified).

Parameters:
  • image – The image to colorize.

  • black – The color to use for black input pixels.

  • white – The color to use for white input pixels.

  • mid – The color to use for midtone input pixels.

  • blackpoint – an int value [0, 255] for the black mapping.

  • whitepoint – an int value [0, 255] for the white mapping.

  • midpoint – an int value [0, 255] for the midtone mapping.

Returns:

An image.

PIL.ImageOps.crop(image, border=0)[source]#

Remove border from image. The same amount of pixels are removed from all four sides. This function works on all image modes.

See also

crop()

Parameters:
  • image – The image to crop.

  • border – The number of pixels to remove.

Returns:

An image.

PIL.ImageOps.scale(image, factor, resample=Resampling.BICUBIC)[source]#

Returns a rescaled image by a specific factor given in parameter. A factor greater than 1 expands the image, between 0 and 1 contracts the image.

Parameters:
  • image – The image to rescale.

  • factor – The expansion factor, as a float.

  • resample – Resampling method to use. Default is BICUBIC. See Filters.

Returns:

An Image object.

PIL.ImageOps.deform(image, deformer, resample=Resampling.BILINEAR)[source]#

Deform the image.

Parameters:
  • image – The image to deform.

  • deformer – A deformer object. Any object that implements a getmesh method can be used.

  • resample – An optional resampling filter. Same values possible as in the PIL.Image.transform function.

Returns:

An image.

PIL.ImageOps.equalize(image, mask=None)[source]#

Equalize the image histogram. This function applies a non-linear mapping to the input image, in order to create a uniform distribution of grayscale values in the output image.

Parameters:
  • image – The image to equalize.

  • mask – An optional mask. If given, only the pixels selected by the mask are included in the analysis.

Returns:

An image.

PIL.ImageOps.expand(image, border=0, fill=0)[source]#

Add border to the image

Parameters:
  • image – The image to expand.

  • border – Border width, in pixels.

  • fill – Pixel fill value (a color value). Default is 0 (black).

Returns:

An image.

PIL.ImageOps.flip(image)[source]#

Flip the image vertically (top to bottom).

Parameters:

image – The image to flip.

Returns:

An image.

PIL.ImageOps.grayscale(image)[source]#

Convert the image to grayscale.

Parameters:

image – The image to convert.

Returns:

An image.

PIL.ImageOps.invert(image)[source]#

Invert (negate) the image.

Parameters:

image – The image to invert.

Returns:

An image.

PIL.ImageOps.mirror(image)[source]#

Flip image horizontally (left to right).

Parameters:

image – The image to mirror.

Returns:

An image.

PIL.ImageOps.posterize(image, bits)[source]#

Reduce the number of bits for each color channel.

Parameters:
  • image – The image to posterize.

  • bits – The number of bits to keep for each channel (1-8).

Returns:

An image.

PIL.ImageOps.solarize(image, threshold=128)[source]#

Invert all pixel values above a threshold.

Parameters:
  • image – The image to solarize.

  • threshold – All pixels above this grayscale level are inverted.

Returns:

An image.

PIL.ImageOps.exif_transpose(image, *, in_place=False)[source]#

If an image has an EXIF Orientation tag, other than 1, transpose the image accordingly, and remove the orientation data.

Parameters:
  • image – The image to transpose.

  • in_place – Boolean. Keyword-only argument. If True, the original image is modified in-place, and None is returned. If False (default), a new Image object is returned with the transposition applied. If there is no transposition, a copy of the image will be returned.

Resize relative to a given size#

from PIL import Image, ImageOps
size = (100, 150)
with Image.open("Tests/images/hopper.png") as im:
    ImageOps.contain(im, size).save("imageops_contain.png")
    ImageOps.cover(im, size).save("imageops_cover.png")
    ImageOps.fit(im, size).save("imageops_fit.png")
    ImageOps.pad(im, size, color="#f00").save("imageops_pad.png")

    # thumbnail() can also be used,
    # but will modify the image object in place
    im.thumbnail(size)
    im.save("imageops_thumbnail.png")

thumbnail()

contain()

cover()

fit()

pad()

Given size

(100, 150)

(100, 150)

(100, 150)

(100, 150)

(100, 150)

Resulting image

../_images/image_thumbnail.png ../_images/imageops_contain.png ../_images/imageops_cover.png ../_images/imageops_fit.png ../_images/imageops_pad.png

Resulting size

100×100

100×100

150×150

100×150

100×150

PIL.ImageOps.contain(image, size, method=Resampling.BICUBIC)[source]#

Returns a resized version of the image, set to the maximum width and height within the requested size, while maintaining the original aspect ratio.

Parameters:
  • image – The image to resize.

  • size – The requested output size in pixels, given as a (width, height) tuple.

  • method – Resampling method to use. Default is BICUBIC. See Filters.

Returns:

An image.

PIL.ImageOps.cover(image, size, method=Resampling.BICUBIC)[source]#

Returns a resized version of the image, so that the requested size is covered, while maintaining the original aspect ratio.

Parameters:
  • image – The image to resize.

  • size – The requested output size in pixels, given as a (width, height) tuple.

  • method – Resampling method to use. Default is BICUBIC. See Filters.

Returns:

An image.

PIL.ImageOps.fit(image, size, method=Resampling.BICUBIC, bleed=0.0, centering=(0.5, 0.5))[source]#

Returns a resized and cropped version of the image, cropped to the requested aspect ratio and size.

This function was contributed by Kevin Cazabon.

Parameters:
  • image – The image to resize and crop.

  • size – The requested output size in pixels, given as a (width, height) tuple.

  • method – Resampling method to use. Default is BICUBIC. See Filters.

  • bleed – Remove a border around the outside of the image from all four edges. The value is a decimal percentage (use 0.01 for one percent). The default value is 0 (no border). Cannot be greater than or equal to 0.5.

  • centering – Control the cropping position. Use (0.5, 0.5) for center cropping (e.g. if cropping the width, take 50% off of the left side, and therefore 50% off the right side). (0.0, 0.0) will crop from the top left corner (i.e. if cropping the width, take all of the crop off of the right side, and if cropping the height, take all of it off the bottom). (1.0, 0.0) will crop from the bottom left corner, etc. (i.e. if cropping the width, take all of the crop off the left side, and if cropping the height take none from the top, and therefore all off the bottom).

Returns:

An image.

PIL.ImageOps.pad(image, size, method=Resampling.BICUBIC, color=None, centering=(0.5, 0.5))[source]#

Returns a resized and padded version of the image, expanded to fill the requested aspect ratio and size.

Parameters:
  • image – The image to resize and crop.

  • size – The requested output size in pixels, given as a (width, height) tuple.

  • method – Resampling method to use. Default is BICUBIC. See Filters.

  • color – The background color of the padded image.

  • centering

    Control the position of the original image within the padded version.

    (0.5, 0.5) will keep the image centered (0, 0) will keep the image aligned to the top left (1, 1) will keep the image aligned to the bottom right

Returns:

An image.