updates to transparency filtering, fixing mass tag edit autocomplete

This commit is contained in:
Bryan Van Deusen
2026-01-21 08:36:46 -05:00
parent ede1457abc
commit fa9b89eca2
6 changed files with 111 additions and 81 deletions
+38 -10
View File
@@ -105,26 +105,54 @@ def is_mostly_transparent(file_path: str, threshold: float = 0.9) -> bool:
"""
Check if an image is mostly transparent (> threshold % of pixels are transparent).
Returns False for images without alpha channel.
Args:
file_path: Path to the image file
threshold: Percentage of pixels that must be transparent (0.0-1.0, default 0.9 = 90%)
Returns:
True if the image has > threshold% transparent pixels
"""
try:
with Image.open(file_path) as img:
if img.mode not in ("RGBA", "LA", "P"):
original_mode = img.mode
# Handle different image modes that may have transparency
if original_mode == "RGBA":
# Already has alpha channel
pass
elif original_mode == "LA":
# Grayscale with alpha
img = img.convert("RGBA")
elif original_mode == "P":
# Palette mode - check for transparency
if "transparency" in img.info:
img = img.convert("RGBA")
else:
return False
elif original_mode == "PA":
# Palette with alpha
img = img.convert("RGBA")
else:
# RGB, L, etc. - no alpha channel
return False
# Convert palette images with transparency
if img.mode == "P" and "transparency" in img.info:
img = img.convert("RGBA")
elif img.mode == "LA":
img = img.convert("RGBA")
elif img.mode != "RGBA":
# Get alpha channel
bands = img.split()
if len(bands) < 4:
return False
# Get alpha channel and count transparent pixels
alpha = img.split()[-1]
alpha = bands[3] # Alpha is the 4th channel (index 3)
alpha_data = alpha.getdata()
total_pixels = alpha.size[0] * alpha.size[1]
if total_pixels == 0:
return False
# Count pixels where alpha < 128 (more than 50% transparent)
transparent_count = sum(1 for p in alpha.getdata() if p < 128)
transparent_count = sum(1 for p in alpha_data if p < 128)
transparency_ratio = transparent_count / total_pixels
return transparency_ratio > threshold
except Exception as e:
print(f"[WARN] Failed to check transparency for {file_path}: {e}")