This commit is contained in:
2025-12-12 13:48:49 +01:00
parent 6c583a6576
commit 8b005065fe
305 changed files with 294 additions and 0 deletions

View File

@@ -0,0 +1,77 @@
from PIL import Image
import math
def process_sheet(input_path, output_path, target_size=(64, 64)):
print(f"Processing sheet {input_path}...")
try:
img = Image.open(input_path).convert("RGBA")
# 1. Crop to main content first to remove heavy borders
bbox = img.getbbox()
if not bbox:
print("Empty image")
return
# We assume 3 cols, 2 rows roughly distributed in the bbox
# But actually, finding the exact grid in a screenshot is hard.
# Let's try to detect 6 separated blobs? No, too complex.
# Simple approach: Crop to bbox, then assume uniform grid.
content = img.crop(bbox)
w, h = content.size
cell_w = w / 3
cell_h = h / 2
frames = []
# Extract 6 frames
for row in range(2):
for col in range(3):
left = col * cell_w
top = row * cell_h
right = left + cell_w
bottom = top + cell_h
cell = content.crop((left, top, right, bottom))
# DO NOT TRIM individual cells! Use the full cell to preserve animation offset.
# Find the best scale to fit the cell into target_size
cw, ch = cell.size
scale = min(target_size[0] / cw, target_size[1] / ch)
new_w = int(cw * scale)
new_h = int(ch * scale)
resized = cell.resize((new_w, new_h), Image.Resampling.LANCZOS)
final_frame = Image.new("RGBA", target_size, (0,0,0,0))
# Center horizontally, align BOTTOM vertically (usually better for feet)
off_x = (target_size[0] - new_w) // 2
off_y = target_size[1] - new_h
# If the original image had a lot of empty space at bottom, feet might float.
# But since we cropped 'content' to global bbox first, it should be tight enough vertically.
final_frame.paste(resized, (off_x, off_y))
frames.append(final_frame)
# Create Strip (6x1)
strip_w = target_size[0] * 6
strip_h = target_size[1]
strip = Image.new("RGBA", (strip_w, strip_h))
for i, frame in enumerate(frames):
strip.paste(frame, (i * target_size[0], 0))
strip.save(output_path)
print(f"Saved strip to {output_path}")
except Exception as e:
print(f"Error: {e}")
process_sheet("assets/sprites/player_walk.png", "assets/sprites/player_walk_strip.png")
process_sheet("assets/sprites/zombie_walk.png", "assets/sprites/zombie_walk_strip.png")

View File

@@ -0,0 +1,38 @@
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")

View File

@@ -0,0 +1,78 @@
from PIL import Image
import os
# Lista slik za procesiranje
images_to_process = [
'assets/tree_purple.png',
'assets/tree_apple.png',
'assets/tree_pear.png',
'assets/tree_cherry.png'
]
def remove_background(image_path):
"""Odstrani ozadje iz slike - naredi transparentno"""
try:
# Naloži sliko
img = Image.open(image_path).convert('RGBA')
data = img.getdata()
new_data = []
for item in data:
r, g, b, a = item
# Izračunaj brightness in grayscale
brightness = (r + g + b) / 3
is_grayscale = abs(r - g) < 20 and abs(g - b) < 20 and abs(r - b) < 20
# Odstrani:
# 1. Vse sive barve (šahovsko ozadje)
# 2. Vse svetle barve (brightness > 150)
# 3. Bele barve
# 4. Pastelne barve (nizka saturacija)
should_remove = False
# Sive barve
if is_grayscale and brightness > 80:
should_remove = True
# Svetle barve
if brightness > 150:
should_remove = True
# Bele
if r > 240 and g > 240 and b > 240:
should_remove = True
# Pastelne (nizka saturacija)
max_rgb = max(r, g, b)
min_rgb = min(r, g, b)
saturation = 0 if max_rgb == 0 else (max_rgb - min_rgb) / max_rgb
if saturation < 0.3 and brightness > 120:
should_remove = True
if should_remove:
new_data.append((r, g, b, 0)) # Transparentno
else:
new_data.append(item) # Ohrani original
# Posodobi sliko
img.putdata(new_data)
# Shrani
img.save(image_path)
print(f'✅ Processed: {image_path}')
except Exception as e:
print(f'❌ Error processing {image_path}: {e}')
# Procesiraj vse slike
print('🎨 Removing backgrounds from tree sprites...')
for img_path in images_to_process:
if os.path.exists(img_path):
remove_background(img_path)
else:
print(f'⚠️ File not found: {img_path}')
print('✅ Done! All backgrounds removed.')

View File

@@ -0,0 +1,77 @@
from PIL import Image
import os
# Lista slik za procesiranje
images_to_process = [
'assets/tree_purple.png',
'assets/tree_apple.png',
'assets/tree_pear.png',
'assets/tree_cherry.png',
'assets/tree_green_final.png',
'assets/tree_blue_final.png',
'assets/tree_dead_final.png'
]
def ultra_remove_background(image_path):
"""ULTRA AGRESIVNO odstranjevanje ozadja"""
try:
# Naloži sliko
img = Image.open(image_path).convert('RGBA')
data = img.getdata()
new_data = []
for item in data:
r, g, b, a = item
# Če je že transparentno, ohrani
if a == 0:
new_data.append(item)
continue
# Izračunaj brightness
brightness = (r + g + b) / 3
# ULTRA AGRESIVNO: Odstrani VSE kar je svetlejše od 100
# To bo odstranilo VSE šahovske kvadratke
if brightness > 100:
new_data.append((r, g, b, 0)) # Transparentno
continue
# Odstrani VSE sive barve (ne glede na brightness)
is_grayscale = abs(r - g) < 25 and abs(g - b) < 25 and abs(r - b) < 25
if is_grayscale:
new_data.append((r, g, b, 0)) # Transparentno
continue
# Ohrani samo temne, nasičene barve (drevesa)
max_rgb = max(r, g, b)
min_rgb = min(r, g, b)
saturation = 0 if max_rgb == 0 else (max_rgb - min_rgb) / max_rgb
# Če je nizka saturacija (pastelno) -> odstrani
if saturation < 0.2:
new_data.append((r, g, b, 0))
continue
# Ohrani pixel
new_data.append(item)
# Posodobi sliko
img.putdata(new_data)
# Shrani
img.save(image_path)
print(f'✅ ULTRA Processed: {image_path}')
except Exception as e:
print(f'❌ Error processing {image_path}: {e}')
# Procesiraj vse slike
print('🔥 ULTRA AGGRESSIVE background removal...')
for img_path in images_to_process:
if os.path.exists(img_path):
ultra_remove_background(img_path)
else:
print(f'⚠️ File not found: {img_path}')
print('✅ Done! All backgrounds ULTRA removed.')