Skip to content

[Term Entry] Python Pillow - Image: .getextrema() #6560

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions content/pillow/concepts/image/terms/getextrema/getextrema.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
---
Title: '.getextrema()'
Description: 'Returns the minimum and maximum pixel values for each band in an image.'
Subjects:
- 'Computer Science'
- 'Data Visualization'
Tags:
- 'Images'
- 'Methods'
- 'Pillow'
- 'Python'
CatalogContent:
- '-learn-python-3'
- 'paths/computer-science'
---

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.

## How It Works

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

## Syntax

```pseudo
Image.getextrema()
```

## Example

The image used in the example is as follows:

![Funny Husky Image](https://raw.githubusercontent.com/Codecademy/docs/main/media/funny-husky.jpg)

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

```py
from PIL import Image

# Open an 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:

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

### Why It’s Useful

- **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 you an insight into the image's contrast.

- **Preprocessing and Analysis:**
Knowing the pixel value range is useful for various 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.

Binary file added media/funny-husky.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading