INNER CODE UNIT · Python

db

KennethJAllen/proper-pixel-art · proper_pixel_art/colors.py:31

    dr, dg, db = a[0] - b[0], a[1] - b[1], a[2] - b[2]
    return dr**2 + dg**2 + db**2


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)]

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…