Skip to content

Instantly share code, notes, and snippets.

@nihirash
Last active February 12, 2022 10:35
Show Gist options
  • Select an option

  • Save nihirash/1d0e55de9f3f1f37acb97d059c3d4c62 to your computer and use it in GitHub Desktop.

Select an option

Save nihirash/1d0e55de9f3f1f37acb97d059c3d4c62 to your computer and use it in GitHub Desktop.
Python script to convert PNGs to RGBA555 image for SAGA chipset(requires PyPNG - to install call pip3 install pypng)
#!/usr/bin/env python3
import png
import sys
if (len(sys.argv) < 3):
print("Usage: png2rgba555 <input.png> <output.raw>")
exit(1)
input_name = sys.argv[1]
output_name = sys.argv[2]
rgba = png.Reader(input_name).asRGBA()
width = rgba[0]
height = rgba[1]
pixels = list(rgba[2])
with open(output_name, "wb") as f:
for y in range(height):
for x in range(width):
r = pixels[y][0 + x*4] >> 3
g = pixels[y][1 + x*4] >> 3
b = pixels[y][2 + x*4] >> 3
a = int(pixels[y][3 + x*4] < 127)
word = (a & 0x1) << 15 | (b & 0x1f) | (g & 0x1f) << 5 | (r & 0x1f) << 10
bytes = word.to_bytes(2, byteorder='big')
f.write(bytes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment