Files
novafarma/process_sprites.py
2025-12-07 04:19:57 +01:00

39 lines
1.3 KiB
Python

from PIL import Image
import os
def remove_background(input_path, output_path):
print(f"Processing {input_path}...")
try:
img = Image.open(input_path).convert("RGBA")
datas = img.getdata()
newData = []
for item in datas:
# Checkerboard colors are usually White (255,255,255) and Light Grey (e.g. 204,204,204 or similar)
# We filter out anything very bright/neutral
r, g, b, a = item
# Check for white/near white
if r > 240 and g > 240 and b > 240:
newData.append((255, 255, 255, 0)) # Transparent
# Check for grey checkerboard (often around 204-240 range)
elif r > 190 and g > 190 and b > 190 and abs(r-g) < 10 and abs(r-b) < 10:
newData.append((255, 255, 255, 0)) # Transparent
else:
newData.append(item)
img.putdata(newData)
# Save
img.save(output_path, "PNG")
print(f"Saved to {output_path}")
except Exception as e:
print(f"Error: {e}")
# Process Player
remove_background("assets/sprites/raw_player.jpg", "assets/sprites/player_walk.png")
# Process Zombie
remove_background("assets/sprites/raw_zombie.jpg", "assets/sprites/zombie_walk.png")