.getextrema()

Anonymous contributor's avatar
Anonymous contributor
Published Apr 30, 2025
Contribute to Docs

The .getextrema() method in the Pillow library quickly finds the minimum and maximum (darkest and brightest) pixel values in an image, showing the range of pixel intensities present.

Syntax

Image.getextrema()

Return value:

  • For single-band images (e.g., grayscale): Returns a tuple (min, max) showing the darkest and brightest pixel values.
  • For multi-band images (e.g., RGB or RGBA): Returns a tuple of tuples like ((min_R, max_R), (min_G, max_G), (min_B, max_B)), providing the minimum and maximum values for each color channel separately.

Example

The image used in the example is:

Funny Husky Image

The following example demonstrates the usage of the .getextrema() method:

from PIL import Image
# Open the image file
cute_dog_image = Image.open("funny_husky.jpg")
# Get extrema for the image
extrema = cute_dog_image.getextrema()
print("Extrema of the image:", extrema)

Here is the output generated by this code:

Extrema of the image: ((0, 255), (0, 255), (0, 255))

Usefulness of .getextrema()

  • Quick Overview of Pixel Value Range: It provides a quick way to understand the overall contrast or brightness spread in an image. For example, if the extrema of a grayscale image are (10, 245), this means the darkest pixel has a value of 10, and the brightest pixel has a value of 245, giving an insight into the image’s contrast.

  • Preprocessing and Analysis: Knowing the pixel value range is useful for several image processing tasks, such as:

    • Adjusting contrast to enhance image features.
    • Performing thresholding to segment objects based on pixel intensity.
    • Identifying potential saturation issues or underexposure in the image.

All contributors

Contribute to Docs

Learn Python:Pillow on Codecademy