85 lines
2.4 KiB
Python
85 lines
2.4 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Remove green background from PNG images and make them transparent.
|
|
Processes all PNG files in the assets directory recursively.
|
|
"""
|
|
|
|
import os
|
|
from PIL import Image
|
|
import numpy as np
|
|
|
|
def remove_green_background(image_path, output_path=None):
|
|
"""
|
|
Remove green background from an image and make it transparent.
|
|
|
|
Args:
|
|
image_path: Path to input image
|
|
output_path: Path to save output (if None, overwrites input)
|
|
"""
|
|
if output_path is None:
|
|
output_path = image_path
|
|
|
|
# Open image
|
|
img = Image.open(image_path)
|
|
img = img.convert("RGBA")
|
|
|
|
# Convert to numpy array
|
|
data = np.array(img)
|
|
|
|
# Get RGB values
|
|
red = data[:, :, 0]
|
|
green = data[:, :, 1]
|
|
blue = data[:, :, 2]
|
|
|
|
# Define green color range (adjust these values if needed)
|
|
# Looking for colors where green is dominant
|
|
green_mask = (
|
|
(green > 100) & # Green channel is bright
|
|
(green > red + 20) & # Green is brighter than red
|
|
(green > blue + 20) # Green is brighter than blue
|
|
)
|
|
|
|
# Set alpha to 0 for green pixels
|
|
data[:, :, 3][green_mask] = 0
|
|
|
|
# Create new image
|
|
result = Image.fromarray(data, mode="RGBA")
|
|
|
|
# Save
|
|
result.save(output_path, "PNG")
|
|
print(f"✓ Processed: {os.path.basename(image_path)}")
|
|
|
|
return True
|
|
|
|
def process_directory(directory):
|
|
"""Process all PNG files in directory recursively."""
|
|
|
|
processed_count = 0
|
|
|
|
for root, dirs, files in os.walk(directory):
|
|
for filename in files:
|
|
if filename.endswith('.png') and not filename.startswith('.'):
|
|
filepath = os.path.join(root, filename)
|
|
try:
|
|
remove_green_background(filepath)
|
|
processed_count += 1
|
|
except Exception as e:
|
|
print(f"✗ Error processing {filename}: {e}")
|
|
|
|
return processed_count
|
|
|
|
if __name__ == "__main__":
|
|
assets_dir = "assets/slike/nova mapa faza 0-1"
|
|
|
|
print("=" * 60)
|
|
print("🎨 GREEN BACKGROUND REMOVAL - PNG Transparency Fix")
|
|
print("=" * 60)
|
|
print(f"\nProcessing directory: {assets_dir}")
|
|
print("\nRemoving green backgrounds...\n")
|
|
|
|
count = process_directory(assets_dir)
|
|
|
|
print("\n" + "=" * 60)
|
|
print(f"✅ COMPLETE: Processed {count} PNG files")
|
|
print("=" * 60)
|