-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount_colors.py
More file actions
34 lines (27 loc) · 969 Bytes
/
count_colors.py
File metadata and controls
34 lines (27 loc) · 969 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from PIL import Image
# Open the image file
im = Image.open("test.png")
# Convert the image to RGB colors
rgb_im = im.convert("RGB")
# Initialize a dictionary to store the colors and their counts
colors = {}
# Get the width and height of the image
width, height = im.size
# Loop over the pixels in the image
for x in range(width):
for y in range(height):
# Get the RGB values for the current pixel
r, g, b = rgb_im.getpixel((x, y))
# Store the color in the dictionary, or increment its count if it's already there
if (r, g, b) in colors:
colors[(r, g, b)] += 1
else:
colors[(r, g, b)] = 1
# Calculate the total number of pixels in the image
total_pixels = width * height
# Loop over the colors in the dictionary
for color in colors:
# Calculate the proportion of pixels that are this color
proportion = colors[color] / total_pixels
# Print the proportion of this color
print(f"Color: {color}, Proportion: {proportion}")