-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.py
More file actions
41 lines (29 loc) · 4.11 KB
/
format.py
File metadata and controls
41 lines (29 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from PIL import Image
from pathlib import Path
INPUT_DIR = Path("tiles")
OUTPUT_DIR = Path("raw")
# SPRITE_SIZE = (16, 16)
OUTPUT_DIR.mkdir(exist_ok=True)
VGA_PALETTE = [ 0, 0, 0, 0, 0, 170, 0, 170, 0, 0, 170, 170, 170, 0, 0, 170, 0, 170, 170, 85, 0, 170, 170, 170, 85, 85, 85, 85, 85, 255, 85, 255, 85, 85, 255, 255, 255, 85, 85, 255, 85, 255, 255, 255, 85, 255, 255, 255, 0, 0, 0, 16, 16, 16, 32, 32, 32, 53, 53, 53, 69, 69, 69, 85, 85, 85, 101, 101, 101, 117, 117, 117, 138, 138, 138, 154, 154, 154, 170, 170, 170, 186, 186, 186, 202, 202, 202, 223, 223, 223, 239, 239, 239, 255, 255, 255, 0, 0, 255, 65, 0, 255, 130, 0, 255, 190, 0, 255, 255, 0, 255, 255, 0, 190, 255, 0, 130, 255, 0, 65, 255, 0, 0, 255, 65, 0, 255, 130, 0, 255, 190, 0, 255, 255, 0, 190, 255, 0, 130, 255, 0, 65, 255, 0, 0, 255, 0, 0, 255, 65, 0, 255, 130, 0, 255, 190, 0, 255, 255, 0, 190, 255, 0, 130, 255, 0, 65, 255, 130, 130, 255, 158, 130, 255, 190, 130, 255, 223, 130, 255, 255, 130, 255, 255, 130, 223, 255, 130, 190, 255, 130, 158, 255, 130, 130, 255, 158, 130, 255, 190, 130, 255, 223, 130, 255, 255, 130, 223, 255, 130, 190, 255, 130, 158, 255, 130, 130, 255, 130, 130, 255, 158, 130, 255, 190, 130, 255, 223, 130, 255, 255, 130, 223, 255, 130, 190, 255, 130, 158, 255, 186, 186, 255, 202, 186, 255, 223, 186, 255, 239, 186, 255, 255, 186, 255, 255, 186, 239, 255, 186, 223, 255, 186, 202, 255, 186, 186, 255, 202, 186, 255, 223, 186, 255, 239, 186, 255, 255, 186, 239, 255, 186, 223, 255, 186, 202, 255, 186, 186, 255, 186, 186, 255, 202, 186, 255, 223, 186, 255, 239, 186, 255, 255, 186, 239, 255, 186, 223, 255, 186, 202, 255, 0, 0, 113, 28, 0, 113, 57, 0, 113, 85, 0, 113, 113, 0, 113, 113, 0, 85, 113, 0, 57, 113, 0, 28, 113, 0, 0, 113, 28, 0, 113, 57, 0, 113, 85, 0, 113, 113, 0, 85, 113, 0, 57, 113, 0, 28, 113, 0, 0, 113, 0, 0, 113, 28, 0, 113, 57, 0, 113, 85, 0, 113, 113, 0, 85, 113, 0, 57, 113, 0, 28, 113, 57, 57, 113, 69, 57, 113, 85, 57, 113, 97, 57, 113, 113, 57, 113, 113, 57, 97, 113, 57, 85, 113, 57, 69, 113, 57, 57, 113, 69, 57, 113, 85, 57, 113, 97, 57, 113, 113, 57, 97, 113, 57, 85, 113, 57, 69, 113, 57, 57, 113, 57, 57, 113, 69, 57, 113, 85, 57, 113, 97, 57, 113, 113, 57, 97, 113, 57, 85, 113, 57, 69, 113, 81, 81, 113, 89, 81, 113, 97, 81, 113, 105, 81, 113, 113, 81, 113, 113, 81, 105, 113, 81, 97, 113, 81, 89, 113, 81, 81, 113, 89, 81, 113, 97, 81, 113, 105, 81, 113, 113, 81, 105, 113, 81, 97, 113, 81, 89, 113, 81, 81, 113, 81, 81, 113, 89, 81, 113, 97, 81, 113, 105, 81, 113, 113, 81, 105, 113, 81, 97, 113, 81, 89, 113, 0, 0, 65, 16, 0, 65, 32, 0, 65, 49, 0, 65, 65, 0, 65, 65, 0, 49, 65, 0, 32, 65, 0, 16, 65, 0, 0, 65, 16, 0, 65, 32, 0, 65, 49, 0, 65, 65, 0, 49, 65, 0, 32, 65, 0, 16, 65, 0, 0, 65, 0, 0, 65, 16, 0, 65, 32, 0, 65, 49, 0, 65, 65, 0, 49, 65, 0, 32, 65, 0, 16, 65, 32, 32, 65, 40, 32, 65, 49, 32, 65, 57, 32, 65, 65, 32, 65, 65, 32, 57, 65, 32, 49, 65, 32, 40, 65, 32, 32, 65, 40, 32, 65, 49, 32, 65, 57, 32, 65, 65, 32, 57, 65, 32, 49, 65, 32, 40, 65, 32, 32, 65, 32, 32, 65, 40, 32, 65, 49, 32, 65, 57, 32, 65, 65, 32, 57, 65, 32, 49, 65, 32, 40, 65, 45, 45, 65, 49, 45, 65, 53, 45, 65, 61, 45, 65, 65, 45, 65, 65, 45, 61, 65, 45, 53, 65, 45, 49, 65, 45, 45, 65, 49, 45, 65, 53, 45, 65, 61, 45, 65, 65, 45, 61, 65, 45, 53, 65, 45, 49, 65, 45, 45, 65, 45, 45, 65, 49, 45, 65, 53, 45, 65, 61, 45, 65, 65, 45, 61, 65, 45, 53, 65, 45, 49, 65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
while len(VGA_PALETTE) < 256 * 3:
VGA_PALETTE += [0, 0, 0]
# create paletted image with VGA palette
pal_img = Image.new("P", (1, 1))
pal_img.putpalette(VGA_PALETTE)
for png_path in INPUT_DIR.glob("*.png"):
print(f"Processing {png_path.name}...")
img = Image.open(png_path).convert("RGB")
# if img.size != SPRITE_SIZE:
# raise ValueError(
# f"{png_path.name} must be {SPRITE_SIZE[0]}x{SPRITE_SIZE[1]} pixels"
# )
# quantize to VGA palette
img = img.quantize(palette=pal_img, dither=Image.NONE)
# extract palette indices
pixels = img.tobytes()
# output .raw file with same name
raw_path = OUTPUT_DIR / (png_path.stem + ".raw")
with open(raw_path, "wb") as f:
f.write(pixels)
print(f"Saved {raw_path}")
print ("All PNG files converted.")