Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
CircularGreenSimCity
CircularGreenSimCity
Commits
06016d93
Commit
06016d93
authored
May 06, 2025
by
Eric Duminil
Browse files
Getting colors from PDF
parent
0f7ec790
Changes
2
Hide whitespace changes
Inline
Side-by-side
get_soil_type_from_bgr/map_colors/labels.png
0 → 100644
View file @
06016d93
34.9 KB
get_soil_type_from_bgr/map_colors/parse_labels.py
0 → 100644
View file @
06016d93
import
sys
# To handle potential errors more gracefully
from
collections
import
Counter
from
PIL
import
Image
def
analyze_image_colors
(
image_path
):
"""
Opens an image, extracts unique RGB colors, and prints them sorted by frequency.
Args:
image_path (str): The path to the image file (e.g., 'diagram.png').
"""
try
:
# Open the image file
img
=
Image
.
open
(
image_path
)
# Ensure the image is in RGB format to get (R, G, B) tuples
# This handles various modes like RGBA (discards alpha), L (grayscale), P (palette)
img_rgb
=
img
.
convert
(
"RGB"
)
# Get all pixel data efficiently. Returns a sequence of (R, G, B) tuples.
pixels
=
img_rgb
.
getdata
()
# Use Counter to efficiently count occurrences of each color tuple
color_counts
=
Counter
(
pixels
)
# Sort the unique colors by their count (frequency) in descending order
# items() gives [(color, count), ...], sort by the second element (count)
sorted_colors
=
sorted
(
color_counts
.
items
(),
key
=
lambda
item
:
item
[
1
],
reverse
=
True
)
# Output the results
print
(
f
"Unique colors found in '
{
image_path
}
', sorted by frequency:"
)
if
not
sorted_colors
:
print
(
"No pixels found or image is empty."
)
else
:
for
color
,
count
in
sorted_colors
:
if
count
<
200
:
continue
html
=
"#%02x%02x%02x"
%
color
print
(
f
"RGB:
{
html
}
, Count:
{
count
}
"
)
except
FileNotFoundError
:
print
(
f
"Error: The file '
{
image_path
}
' was not found."
,
file
=
sys
.
stderr
)
except
Exception
as
e
:
print
(
f
"An error occurred:
{
e
}
"
,
file
=
sys
.
stderr
)
# RGB: #fffcd6, Count: 2015
# RGB: #fffa91, Count: 2015
# RGB: #fff500, Count: 2046
# RGB: #d99e5e, Count: 2046
# RGB: #b8663d, Count: 2046
# RGB: #b68041, Count: 2046
# RGB: #ffa67d, Count: 2015
# RGB: #e87845, Count: 2015
# RGB: #f299bf, Count: 2015
# RGB: #70a800, Count: 2015
# RGB: #d9f2ff, Count: 2016
cmap
=
{
"Abbauflächen"
:
"#222222"
,
"Normallehme (ll)"
:
"#b8663d"
,
"Lehmsande (ls)"
:
"#fffa91"
,
"Lehmschluffe (lu)"
:
"#ffa67d"
,
"Moore"
:
"#70a800"
,
"Sandlehme (sl)"
:
"#d99e5e"
,
"Reinsande (ss)"
:
"#fffcd6"
,
"Tonlehme (tl)"
:
"#b68041"
,
"Tonschluffe (tu)"
:
"#e87845"
,
"Schluffsande (us)"
:
"#fff500"
,
"Schlufftone (ut)"
:
"#f299bf"
,
"Watt"
:
"#d9f2ff"
,
"Siedlung"
:
"#222222"
,
"Gewässer"
:
"#0000ff"
,
}
# --- Main execution ---
if
__name__
==
"__main__"
:
image_file
=
"labels.png"
# The name of your image file
analyze_image_colors
(
image_file
)
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment