#!/usr/bin/env python3 """ Quick test: Chroma key on 5 images only """ import os import shutil from pathlib import Path from PIL import Image import numpy as np # Test images TEST_IMAGES = [ "assets/PHASE_PACKS/1_FAZA_1/tools/wood/watering_can.png", "assets/PHASE_PACKS/1_FAZA_1/animals/horse.png", "assets/PHASE_PACKS/1_FAZA_1/infrastructure/farm_elements/manure_pile.png", "assets/PHASE_PACKS/1_FAZA_1/tools/iron/pickaxe.png", "assets/PHASE_PACKS/1_FAZA_1/animals/sheep/walk.png", ] OUTPUT_DIR = "test_transparency" def chroma_key_process(input_path, output_path): """Full chroma key pipeline on single image""" img = Image.open(input_path) if img.mode != 'RGBA': img = img.convert('RGBA') data = np.array(img).astype(np.float32) r, g, b, a = data[:,:,0], data[:,:,1], data[:,:,2], data[:,:,3] # Step 1: Detect white/black/gray backgrounds # Pure white is_white = (r > 250) & (g > 250) & (b > 250) # Pure black is_black = (r < 5) & (g < 5) & (b < 5) # Near-white/gray (anti-aliasing) is_near_white = (r > 245) & (g > 245) & (b > 245) # Background mask background = is_white | is_black | is_near_white # Step 2: Set alpha to 0 for background pixels new_alpha = np.where(background, 0, a) # Step 3: For semi-white pixels (anti-aliasing), fade alpha brightness = (r + g + b) / 3 semi_white = (brightness > 240) & (brightness <= 250) # Gradual fade based on brightness fade_alpha = np.where(semi_white, (255 - brightness) * 10, new_alpha) fade_alpha = np.clip(fade_alpha, 0, 255) data[:,:,3] = fade_alpha # Clean up fully transparent pixels fully_transparent = fade_alpha < 5 data[fully_transparent, 0] = 0 data[fully_transparent, 1] = 0 data[fully_transparent, 2] = 0 data[fully_transparent, 3] = 0 result = Image.fromarray(data.astype(np.uint8)) result.save(output_path, 'PNG') print(f" โ Processed: {os.path.basename(output_path)}") def main(): print("๐งช QUICK TEST: Chroma Key on 5 Images") print("=" * 50) # Create output dir os.makedirs(OUTPUT_DIR, exist_ok=True) processed = [] for img_path in TEST_IMAGES: if not os.path.exists(img_path): print(f" โ Not found: {img_path}") continue name = os.path.basename(img_path) name_no_ext = os.path.splitext(name)[0] # Copy original orig_dest = os.path.join(OUTPUT_DIR, f"{name_no_ext}_ORIGINAL.png") shutil.copy(img_path, orig_dest) print(f" ๐ Copied original: {name}") # Process and save proc_dest = os.path.join(OUTPUT_DIR, f"{name_no_ext}_PROCESSED.png") chroma_key_process(img_path, proc_dest) processed.append({ 'name': name_no_ext, 'original': orig_dest, 'processed': proc_dest }) # Generate comparison HTML html = '''
Left: Original (white/black background) | Right: Processed (transparent with checkerboard)
''' for item in processed: html += f'''If you see the gray checkerboard pattern behind the processed images, transparency is working! ๐
''' html_path = os.path.join(OUTPUT_DIR, "comparison.html") with open(html_path, 'w') as f: f.write(html) print("\n" + "=" * 50) print(f"โ DONE! Created comparison page:") print(f" ๐ {OUTPUT_DIR}/") print(f" ๐ Open: {html_path}") print("=" * 50) if __name__ == '__main__': main()