37 lines
1.2 KiB
Python
37 lines
1.2 KiB
Python
from PIL import Image
|
|
|
|
def remove_background(input_path, output_path):
|
|
"""Remove background and make it transparent"""
|
|
img = Image.open(input_path).convert("RGBA")
|
|
datas = img.getdata()
|
|
|
|
# Get background color from corner pixel
|
|
bg_color = datas[0][:3] # RGB from top-left
|
|
|
|
# Create new data with transparent background
|
|
new_data = []
|
|
tolerance = 50 # Color similarity tolerance
|
|
|
|
for item in datas:
|
|
# Check if pixel is similar to background
|
|
r_diff = abs(item[0] - bg_color[0])
|
|
g_diff = abs(item[1] - bg_color[1])
|
|
b_diff = abs(item[2] - bg_color[2])
|
|
|
|
if r_diff < tolerance and g_diff < tolerance and b_diff < tolerance:
|
|
# Make background transparent
|
|
new_data.append((item[0], item[1], item[2], 0))
|
|
else:
|
|
# Keep original pixel
|
|
new_data.append(item)
|
|
|
|
img.putdata(new_data)
|
|
img.save(output_path)
|
|
print(f"✅ Transparent: {output_path}")
|
|
|
|
# Process tiles
|
|
remove_background('c:/novafarma/assets/grass_tile.png', 'c:/novafarma/assets/grass_tile.png')
|
|
remove_background('c:/novafarma/assets/water_tile.png', 'c:/novafarma/assets/water_tile.png')
|
|
|
|
print("🎉 Tile backgrounds removed!")
|