Files
novafarma/scripts/remove_green_background.py
NovaFarma Dev 0985af29e5 Green Background Removal + TSX Generation + Bug Fixes
Removed green backgrounds from 30 tileset PNGs (16.4M pixels!)
 Created mass TSX generation script for 3877 individual objects
 Fixed TiledTestScene cursor crash bug
 Added micro_farm_8x8 JSON loading support
 Documentation: GREEN_BACKGROUND_FIX.md, MASS_TSX_GENERATION.md

Scripts:
- scripts/remove_green_background.py (batch transparency fix)
- scripts/generate_mass_tsx.py (3877 .tsx files generator)

Backups: assets/tilesets/backup_green_bg/
2025-12-22 11:23:25 +01:00

121 lines
3.3 KiB
Python

#!/usr/bin/env python3
"""
NovaFarma - Batch Remove Green Background
==========================================
This script removes bright green (#00FF00) backgrounds from all PNG images
in the assets/tilesets/ directory and replaces them with transparency.
Author: Antigravity AI
Date: 2025-12-22
"""
import os
from PIL import Image
import shutil
from pathlib import Path
# Configuration
TILESETS_DIR = r"c:\novafarma\assets\tilesets"
BACKUP_DIR = r"c:\novafarma\assets\tilesets\backup_green_bg"
GREEN_TOLERANCE = 30 # RGB tolerance for green detection
def is_green_pixel(r, g, b, tolerance=GREEN_TOLERANCE):
"""
Check if a pixel is bright green (#00FF00 or similar).
Returns True if the pixel is greenish.
"""
# Bright green: high G, low R and B
return (g > 200 and r < tolerance and b < tolerance)
def remove_green_background(image_path, output_path):
"""
Remove bright green background from an image and make it transparent.
"""
print(f"Processing: {os.path.basename(image_path)}")
# Open image
img = Image.open(image_path)
# Convert to RGBA if not already
if img.mode != 'RGBA':
img = img.convert('RGBA')
# Get pixel data
pixels = img.load()
width, height = img.size
# Process each pixel
changed_pixels = 0
for y in range(height):
for x in range(width):
r, g, b, a = pixels[x, y]
# If pixel is green, make it transparent
if is_green_pixel(r, g, b):
pixels[x, y] = (r, g, b, 0) # Set alpha to 0
changed_pixels += 1
# Save the result
img.save(output_path, 'PNG')
print(f" ✅ Done! Changed {changed_pixels:,} pixels to transparent")
return changed_pixels
def main():
"""
Main function to process all PNG files in tilesets directory.
"""
print("=" * 60)
print("NovaFarma - Batch Green Background Removal")
print("=" * 60)
print()
# Create backup directory
os.makedirs(BACKUP_DIR, exist_ok=True)
print(f"✅ Backup directory: {BACKUP_DIR}")
print()
# Find all PNG files
png_files = list(Path(TILESETS_DIR).glob("*.png"))
print(f"Found {len(png_files)} PNG files to process\n")
if len(png_files) == 0:
print("❌ No PNG files found!")
return
# Process each file
total_changed = 0
processed_count = 0
for png_file in png_files:
input_path = str(png_file)
filename = png_file.name
backup_path = os.path.join(BACKUP_DIR, filename)
# Create backup
shutil.copy2(input_path, backup_path)
# Remove green background
try:
changed = remove_green_background(input_path, input_path)
total_changed += changed
processed_count += 1
except Exception as e:
print(f" ❌ ERROR: {e}")
# Summary
print()
print("=" * 60)
print("SUMMARY")
print("=" * 60)
print(f"✅ Processed: {processed_count}/{len(png_files)} files")
print(f"✅ Total pixels changed: {total_changed:,}")
print(f"✅ Backups saved to: {BACKUP_DIR}")
print()
print("Done! All green backgrounds removed! 🎉")
print("=" * 60)
if __name__ == "__main__":
main()