28 lines
1016 B
Python
28 lines
1016 B
Python
"""Transparency audit: matches images whose transparent-pixel fraction
|
|
exceeds the threshold. Animated images short-circuit (skipped) to avoid
|
|
the multi-frame PIL decode that hits Celery's hard time limit."""
|
|
|
|
|
|
def evaluate(pil_image, *, threshold: float) -> bool:
|
|
"""True iff the image's transparent-pixel fraction exceeds threshold.
|
|
|
|
False for non-alpha modes and animated images. Mirrors the import-side
|
|
Importer._transparency_pct logic so retroactive enforcement matches
|
|
prospective filtering.
|
|
"""
|
|
if getattr(pil_image, "is_animated", False):
|
|
return False
|
|
if pil_image.mode not in ("RGBA", "LA") and not (
|
|
pil_image.mode == "P" and "transparency" in pil_image.info
|
|
):
|
|
return False
|
|
im = pil_image
|
|
if im.mode != "RGBA":
|
|
im = im.convert("RGBA")
|
|
alpha = im.getchannel("A")
|
|
histogram = alpha.histogram()
|
|
transparent = histogram[0]
|
|
total = sum(histogram)
|
|
pct = transparent / total if total else 0.0
|
|
return pct > threshold
|