Optimize bg.png conversion without scaling and with correct color mapping

This commit is contained in:
2026-06-23 11:10:17 +02:00
parent f00b533581
commit 0ceae6f98b
7 changed files with 273 additions and 198 deletions
+30 -9
View File
@@ -3,20 +3,41 @@ from PIL import Image
# Open the new cover image
img = Image.open('bg.png')
# Resize to 128x128 to ensure maximum 256 unique tiles
img = img.resize((128, 128), Image.Resampling.LANCZOS)
# Convert to grayscale first to get raw intensity values
img_gray = img.convert('L')
# Create a black 160x144 background
bg = Image.new('L', (160, 144), color=0)
# Create a new indexed image with the exact Game Boy palette mapping
# 0 = White, 1 = Light Gray, 2 = Dark Gray, 3 = Black
final_img = Image.new('P', img_gray.size)
# Paste the resized image in the center
bg.paste(img, (16, 8))
# Set the palette
palette = [
255, 255, 255, # 0: White
170, 170, 170, # 1: Light Gray
85, 85, 85, # 2: Dark Gray
0, 0, 0 # 3: Black
]
palette += [0] * (256 * 3 - len(palette))
final_img.putpalette(palette)
# Quantize to 4 colors (indexed mode 'P') for png2asset
final_img = bg.quantize(colors=4)
# Map the grayscale pixels to palette indices (0-3)
# Darker pixels get higher indices (closer to 3/Black), lighter get lower indices (closer to 0/White)
raw_pixels = list(img_gray.getdata())
mapped_pixels = []
for p in raw_pixels:
if p > 200:
mapped_pixels.append(0) # White
elif p > 120:
mapped_pixels.append(1) # Light Gray
elif p > 55:
mapped_pixels.append(2) # Dark Gray
else:
mapped_pixels.append(3) # Black
final_img.putdata(mapped_pixels)
# Save the resulting image
final_img.save('title_bg.png')
print("title_bg.png created successfully (160x144, indexed).")
print("title_bg.png created successfully with correct color indexing.")