202 lines
5.9 KiB
Python
202 lines
5.9 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Green Screen Generator - Creates images with #00FF00 background
|
|
Then can convert green to alpha for clean transparency
|
|
"""
|
|
|
|
import os
|
|
from pathlib import Path
|
|
from rembg import remove
|
|
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"
|
|
CHROMA_GREEN = (0, 255, 0, 255) # #00FF00
|
|
|
|
def add_green_background(input_path, output_green_path, output_alpha_path):
|
|
"""
|
|
1. Use AI to remove background
|
|
2. Add solid green (#00FF00) background
|
|
3. Also save version with alpha transparency
|
|
"""
|
|
img = Image.open(input_path)
|
|
|
|
# Step 1: AI remove background -> get alpha mask
|
|
img_no_bg = remove(img)
|
|
|
|
# Save alpha version
|
|
img_no_bg.save(output_alpha_path, 'PNG')
|
|
|
|
# Step 2: Create green background version
|
|
# Create solid green canvas
|
|
green_bg = Image.new('RGBA', img_no_bg.size, CHROMA_GREEN)
|
|
|
|
# Composite the subject onto green background
|
|
green_bg.paste(img_no_bg, (0, 0), img_no_bg)
|
|
|
|
# Convert to RGB (no alpha) for clean green screen look
|
|
green_rgb = green_bg.convert('RGB')
|
|
green_rgb.save(output_green_path, 'PNG')
|
|
|
|
print(f" ✅ {os.path.basename(input_path)}")
|
|
print(f" → Green: {os.path.basename(output_green_path)}")
|
|
print(f" → Alpha: {os.path.basename(output_alpha_path)}")
|
|
|
|
def main():
|
|
print("🟢 GREEN SCREEN GENERATOR")
|
|
print("=" * 50)
|
|
print("\nCreating images with #00FF00 chroma key background")
|
|
print("Like the zombie bunny reference!\n")
|
|
|
|
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]
|
|
|
|
green_dest = os.path.join(OUTPUT_DIR, f"{name_no_ext}_GREEN.png")
|
|
alpha_dest = os.path.join(OUTPUT_DIR, f"{name_no_ext}_ALPHA.png")
|
|
|
|
add_green_background(img_path, green_dest, alpha_dest)
|
|
|
|
processed.append({
|
|
'name': name_no_ext,
|
|
'original': f"{name_no_ext}_ORIGINAL.png",
|
|
'green': f"{name_no_ext}_GREEN.png",
|
|
'alpha': f"{name_no_ext}_ALPHA.png"
|
|
})
|
|
|
|
# Generate comparison HTML
|
|
html = '''<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>🟢 Green Screen Test</title>
|
|
<style>
|
|
body {
|
|
background: #1a1a2e;
|
|
color: white;
|
|
font-family: Arial, sans-serif;
|
|
padding: 30px;
|
|
}
|
|
h1 { color: #00ff00; text-align: center; text-shadow: 2px 2px #000; }
|
|
.note {
|
|
background: #00ff00;
|
|
color: black;
|
|
padding: 15px;
|
|
border-radius: 10px;
|
|
text-align: center;
|
|
margin-bottom: 30px;
|
|
font-weight: bold;
|
|
}
|
|
.comparison {
|
|
display: flex;
|
|
gap: 20px;
|
|
margin: 25px 0;
|
|
padding: 20px;
|
|
background: rgba(0,0,0,0.4);
|
|
border-radius: 15px;
|
|
align-items: center;
|
|
}
|
|
.image-box {
|
|
flex: 1;
|
|
text-align: center;
|
|
}
|
|
.image-box h3 { color: #aaa; margin-bottom: 10px; font-size: 0.9em; }
|
|
.image-box img {
|
|
max-width: 200px;
|
|
max-height: 200px;
|
|
border-radius: 8px;
|
|
border: 2px solid #333;
|
|
}
|
|
.green-bg {
|
|
background: #00ff00;
|
|
padding: 10px;
|
|
border-radius: 8px;
|
|
}
|
|
.checker-bg {
|
|
background: linear-gradient(45deg, #333 25%, #555 25%, #555 50%, #333 50%, #333 75%, #555 75%);
|
|
background-size: 16px 16px;
|
|
padding: 10px;
|
|
border-radius: 8px;
|
|
}
|
|
.grass-bg {
|
|
background: linear-gradient(to bottom, #4a7c23, #2d4d15);
|
|
padding: 10px;
|
|
border-radius: 8px;
|
|
}
|
|
.label { font-size: 0.75em; color: #666; margin-top: 5px; }
|
|
.highlight { color: #00ff00; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<h1>🟢 GREEN SCREEN WORKFLOW</h1>
|
|
<div class="note">
|
|
Workflow: Original → AI Remove BG → Green Screen (#00FF00) → Alpha Transparency
|
|
</div>
|
|
'''
|
|
|
|
for item in processed:
|
|
html += f'''
|
|
<div class="comparison">
|
|
<div class="image-box">
|
|
<h3>📷 ORIGINAL</h3>
|
|
<div class="checker-bg">
|
|
<img src="{item['original']}" alt="Original">
|
|
</div>
|
|
<div class="label">Source file</div>
|
|
</div>
|
|
<div class="image-box">
|
|
<h3 class="highlight">🟢 GREEN SCREEN</h3>
|
|
<div class="green-bg">
|
|
<img src="{item['green']}" alt="Green">
|
|
</div>
|
|
<div class="label">#00FF00 background</div>
|
|
</div>
|
|
<div class="image-box">
|
|
<h3>✅ ALPHA (Checker)</h3>
|
|
<div class="checker-bg">
|
|
<img src="{item['alpha']}" alt="Alpha">
|
|
</div>
|
|
<div class="label">Transparent</div>
|
|
</div>
|
|
<div class="image-box">
|
|
<h3>🌿 IN GAME</h3>
|
|
<div class="grass-bg">
|
|
<img src="{item['alpha']}" alt="Game">
|
|
</div>
|
|
<div class="label">On grass</div>
|
|
</div>
|
|
</div>
|
|
'''
|
|
|
|
html += '''
|
|
</body>
|
|
</html>
|
|
'''
|
|
|
|
html_path = os.path.join(OUTPUT_DIR, "green_screen_test.html")
|
|
with open(html_path, 'w') as f:
|
|
f.write(html)
|
|
|
|
print("\n" + "=" * 50)
|
|
print(f"✅ DONE! Open: {html_path}")
|
|
|
|
if __name__ == '__main__':
|
|
main()
|