Skip to content

add pillow terms getcolors #6559

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 5 commits into from
Apr 30, 2025
Merged
Changes from 1 commit
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
Prev Previous commit
Minor changes
  • Loading branch information
Sriparno08 committed Apr 30, 2025
commit 6654a1db73eb6edb9cb68ed86651b18b0b4b0a0f
19 changes: 10 additions & 9 deletions content/pillow/concepts/image/terms/getcolors/getcolors.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,32 @@ Image.getcolors(maxcolors=256)

**Return value:**

Returns a list of tuples. Each tuple is of the form (count, color):
- `count`: Number of pixels with that color
- `color`: The color value (e.g., RGB or grayscale)
Returns a list of tuples. Each tuple is of the form `(count, color)`:

- `count`: Number of pixels with that color.
- `color`: The color value (e.g., RGB or grayscale).

## Example

The image to be used for this example is:

![Input image to perform the .getcolors() operation](https://raw.githubusercontent.com/Codecademy/docs/main/media/samandgos.jpg)

In this example, the `.getcolors()` method returns a list of (count, color) tuples representing the number of times each color occurs in the image:
In this example, the `.getcolors()` method returns a list of `(count, color)` tuples representing the number of times each color occurs in the image:

```py
from PIL import Image

# Open an image
# Open the image
image = Image.open("samandgos.jpg")

# Convert to RGB
# Convert the image to RGB
image = image.convert("RGB")

# Reduce to 256 colors
# Reduce the image to 256 colors
image = image.quantize(colors=256)

# Now get colors
# Use the .getcolors() method
colors = image.getcolors(maxcolors=256)

if colors is None:
Expand All @@ -63,7 +64,7 @@ else:
print(f"Color {color} occurs {count} times")
```

The output lists each color in the simplified image along with the number of times it occurs. The output produced by this code will be:
The output lists each color in the simplified image along with the number of times it occurs. The output for this example will be:

```shell
Color 0 occurs 5351 times
Expand Down