forked from pimoroni/pimoroni-pico
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspritesheet-to-rgb332.py
37 lines (27 loc) · 975 Bytes
/
spritesheet-to-rgb332.py
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
#!/bin/env python3
from PIL import Image
import numpy
import sys
import pathlib
# Run with `./filename.py source-image.jpg`
IMAGE_PATH = pathlib.Path(sys.argv[1])
OUTPUT_PATH = IMAGE_PATH.with_suffix(".rgb332")
def image_to_data(image):
"""Generator function to convert a PIL image to 16-bit 565 RGB bytes."""
# NumPy is much faster at doing this. NumPy code provided by:
# Keith (https://www.blogger.com/profile/02555547344016007163)
pb = numpy.array(image.convert('RGBA')).astype('uint16')
r = (pb[:, :, 0] & 0b11100000) >> 0
g = (pb[:, :, 1] & 0b11100000) >> 3
b = (pb[:, :, 2] & 0b11000000) >> 6
a = pb[:, :, 3] # Discard
# AAAA RRRR GGGG BBBB
color = r | g | b
return color.astype("uint8").flatten().tobytes()
img = Image.open(IMAGE_PATH)
w, h = img.size
data = image_to_data(img)
print(f"Converted: {w}x{h} {len(data)} bytes")
with open(OUTPUT_PATH, "wb") as f:
f.write(data)
print(f"Written to: {OUTPUT_PATH}")