Files
novafarma/scripts/utils/verify_transparency.py
2026-01-25 12:20:50 +01:00

61 lines
2.4 KiB
Python

import os
from PIL import Image
import numpy as np
def verify_and_clean_hard(file_path):
if not os.path.exists(file_path):
print(f"File not found: {file_path}")
return
try:
img = Image.open(file_path).convert("RGBA")
data = np.array(img)
# Check if we have alpha 0 pixels (transparency)
transparent_pixels = np.sum(data[:,:,3] == 0)
total_pixels = data.shape[0] * data.shape[1]
transparency_ratio = transparent_pixels / total_pixels
print(f"Checking {os.path.basename(file_path)}...")
print(f" - Transparency: {transparency_ratio*100:.2f}%")
# If transparency is 0%, it means the previous script failed or didn't save correctly
# Or the image was fully opaque to begin with.
if transparency_ratio < 0.01: # Less than 1% transparent is suspicious for a cutout
print(" - WARNING: Almost no transparency detected. Retrying forced cleanup...")
# Force Alpha channel to 0 for specific colors again (Harder threshold)
r, g, b, a = data[:,:,0], data[:,:,1], data[:,:,2], data[:,:,3]
# Target Checkerboard Grey/White AND solid Black (just in case)
mask_bad = (r > 190) & (g > 190) & (b > 190)
data[mask_bad, 3] = 0
# Target Green Screen again
mask_green = (g > 200) & (r < 100) & (b < 100)
data[mask_green, 3] = 0
new_img = Image.fromarray(data)
new_img.save(file_path)
print(" - Re-saved with forced transparency.")
else:
print(" - OK: Transparency detected.")
except Exception as e:
print(f" - Error: {e}")
# Check the files we care about
files = [
"/Users/davidkotnik/repos/novafarma/assets/slike/NOVE_SLIKE/UI/okvir_zarjavel.png",
"/Users/davidkotnik/repos/novafarma/assets/slike/NOVE_SLIKE/UI/merilec_zdravja.png",
"/Users/davidkotnik/repos/novafarma/assets/slike/NOVE_SLIKE/UI/amnezija_maska.png",
"/Users/davidkotnik/repos/novafarma/assets/slike/NOVE_SLIKE/UI/gumb_start.png",
"/Users/davidkotnik/repos/novafarma/assets/slike/NOVE_SLIKE/Characters/starsa/Ghost/ghost_otac_cyan.png",
"/Users/davidkotnik/repos/novafarma/assets/slike/NOVE_SLIKE/Characters/starsa/Ghost/MOJE_SLIKE_KONCNA_ostalo_parents_transparent_ghosts_clean.png"
]
for f in files:
verify_and_clean_hard(f)