.getextrema()
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:
The following example demonstrates the usage of the .getextrema()
method:
from PIL import Image# Open the image filecute_dog_image = Image.open("funny_husky.jpg")# Get extrema for the imageextrema = 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 of10
, and the brightest pixel has a value of245
, 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
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:Pillow on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours