Green screen workflow tested and working - rembg AI background removal creates clean #00FF00 backgrounds like reference
This commit is contained in:
204
scripts/test_transparency_5.py
Normal file
204
scripts/test_transparency_5.py
Normal file
@@ -0,0 +1,204 @@
|
||||
#!/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 = '''<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>🧪 Test Transparency - 5 Sample Images</title>
|
||||
<style>
|
||||
body {
|
||||
background: #1a1a2e;
|
||||
color: white;
|
||||
font-family: Arial, sans-serif;
|
||||
padding: 30px;
|
||||
}
|
||||
h1 {
|
||||
color: #ff4444;
|
||||
text-align: center;
|
||||
}
|
||||
.comparison {
|
||||
display: flex;
|
||||
gap: 40px;
|
||||
margin: 30px 0;
|
||||
padding: 20px;
|
||||
background: rgba(0,0,0,0.3);
|
||||
border-radius: 15px;
|
||||
}
|
||||
.image-box {
|
||||
flex: 1;
|
||||
text-align: center;
|
||||
}
|
||||
.image-box h3 {
|
||||
color: #ff6666;
|
||||
margin-bottom: 15px;
|
||||
}
|
||||
.image-box img {
|
||||
max-width: 300px;
|
||||
max-height: 300px;
|
||||
border: 2px solid #333;
|
||||
border-radius: 10px;
|
||||
}
|
||||
/* Checkerboard background for transparency */
|
||||
.transparent-bg {
|
||||
background: linear-gradient(45deg, #333 25%, transparent 25%),
|
||||
linear-gradient(-45deg, #333 25%, transparent 25%),
|
||||
linear-gradient(45deg, transparent 75%, #333 75%),
|
||||
linear-gradient(-45deg, transparent 75%, #333 75%);
|
||||
background-size: 20px 20px;
|
||||
background-position: 0 0, 0 10px, 10px -10px, -10px 0px;
|
||||
background-color: #555;
|
||||
padding: 20px;
|
||||
border-radius: 10px;
|
||||
}
|
||||
.label {
|
||||
margin-top: 10px;
|
||||
font-size: 0.9em;
|
||||
color: #aaa;
|
||||
}
|
||||
.success { color: #44ff44; }
|
||||
.info {
|
||||
text-align: center;
|
||||
color: #888;
|
||||
margin-bottom: 30px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>🧪 Test Transparency - Before & After</h1>
|
||||
<p class="info">Left: Original (white/black background) | Right: Processed (transparent with checkerboard)</p>
|
||||
'''
|
||||
|
||||
for item in processed:
|
||||
html += f'''
|
||||
<div class="comparison">
|
||||
<div class="image-box">
|
||||
<h3>📷 ORIGINAL</h3>
|
||||
<img src="{item['original']}" alt="Original">
|
||||
<div class="label">{item['name']}_ORIGINAL.png</div>
|
||||
</div>
|
||||
<div class="image-box">
|
||||
<h3 class="success">✅ PROCESSED</h3>
|
||||
<div class="transparent-bg">
|
||||
<img src="{item['processed']}" alt="Processed">
|
||||
</div>
|
||||
<div class="label">{item['name']}_PROCESSED.png</div>
|
||||
</div>
|
||||
</div>
|
||||
'''
|
||||
|
||||
html += '''
|
||||
<p class="info" style="margin-top: 40px;">
|
||||
If you see the gray checkerboard pattern behind the processed images, transparency is working! 🎉
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
'''
|
||||
|
||||
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()
|
||||
Reference in New Issue
Block a user