generated from TinyTapeout/tt05-submission-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprite2bit.py
executable file
·33 lines (24 loc) · 982 Bytes
/
sprite2bit.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
from PIL import Image
import glob, os
for image_filename in glob.glob("sprites/*.png"):
print(f'Creating sprite from {image_filename}')
file_name = os.path.splitext(os.path.basename(image_filename))[0]
img = Image.open('sprites/' + file_name + '.png')
pix = img.load()
width, height = img.size
sprite_verilog = ""
sprite_python = "SPRITE = [\n"
for y in range(height):
pixel_row = ''
for x in range(width):
if (pix[x, y] == (255, 255, 255, 255) or pix[x, y] == (255, 255, 255)):
pixel_row += '0'
else:
pixel_row += '1'
sprite_verilog += f"sprite_data[{width*(y+1)-1: <3}: {width*y: <3}] <= {width}'b{pixel_row[::-1]};\n"
sprite_python += f" [{ ','.join(pixel_row) }],\n"
sprite_python += "]"
print("Sprite data Verilog:\n")
print(sprite_verilog)
print("Sprite data Python:\n")
print(sprite_python)