INNER CODE UNIT · Python
top_opaque_colors
KennethJAllen/proper-pixel-art · proper_pixel_art/colors.py:35
def top_opaque_colors(
img: Image.Image,
alpha_threshold: int,
limit: int = _DEFAULTS.top_colors_limit,
thumbnail_size: tuple[int, int] = _DEFAULTS.thumbnail_size,
) -> list[RGB]:
"""Return the most common opaque colors (RGB) up to limit."""
rgba = img.convert("RGBA").copy()
rgba.thumbnail(thumbnail_size) # speed and de-noise tiny details
counts = Counter()
for r, g, b, a in rgba.get_flattened_data():
if a >= alpha_threshold:
counts[(r, g, b)] += 1
return [c for c, _ in counts.most_common(limit)]
DEFAULT_BACKGROUND_CANDIDATES: list[RGB] = [
(0, 255, 255), # cyan