Skip to content

New [Term Entry] Python Pillow - Image: .getbbox() #6671

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
73 changes: 73 additions & 0 deletions content/pillow/concepts/image/terms/getbbox/getbbox.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
---
Title: '.getbbox()'
Description: 'Returns the bounding box of the non-zero regions in the image.'
Subjects:
- 'Computer Science'
- 'Data Visualization'
Tags:
- 'Images'
- 'Methods'
- 'Pillow'
CatalogContent:
- 'learn-python-3'
- 'paths/computer-science'
---

In the Pillow library, the **`.getbbox()`** method returns the bounding box of all non-zero (non-background) regions in an image. It is a feature for cropping or focusing on the area of interest within an image.

If the image is empty (all pixels are zero), `.getbbox()` returns `None`.

## Common Use Cases

The `.getbbox()` method is particularly useful for:

- **Automatic cropping**: Remove unnecessary blank borders around images to focus on the actual content
- **Content detection**: Identify where the meaningful content is located within an image
- **Optimization**: Reduce file size by trimming away empty space before saving
- **Layout analysis**: Determine the boundaries of content regions for further processing
- **Document scanning**: Detect and extract the actual document area from a scanned image

## Syntax

```pseudo
Image.getbbox()
```

**Parameters:**

The `.getbbox()` methods takes no parameters.

**Return Value:**

Returns a 4-[tuple](https://www.codecademy.com/resources/docs/python/tuples) (left, upper, right, lower) that defines the rectangular bounding box containing all non-zero pixels.

Returns `None` if the image is completely zero-valued.

## Example

The image to be used for this example is:

![Input image to perform the .getbbox() operation](https://onetreeplanted.org/cdn/shop/articles/nature_facts_1788x.jpg?v=1705008496)

In this example, the `.getbbox()` method returns the non-zero region of an image:

```py
from PIL import Image

# Open the image file
img = Image.open("nature.png")

# Get the bounding box of the non-zero areas
bbox = img.getbbox()

print(bbox)
```

If the image has content, the above code produces the following output:

```
shell
(0, 0, 1788, 850)
```

The output indicates that the non-zero (non-backround) area of the image starts at (0, 0) and extends to (1788, 850).
Binary file added media/nature.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.