Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@flatduckrecords
Created June 1, 2023 23:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flatduckrecords/ad20d18ea1048c1d6db6fb348ddaa76c to your computer and use it in GitHub Desktop.
Save flatduckrecords/ad20d18ea1048c1d6db6fb348ddaa76c to your computer and use it in GitHub Desktop.
SAM Coupé PNG bitmap converter
#!/usr/bin/python
from PIL import Image # open PNG and get raw (bitmap) data bytes
import struct # we use struct to convert between integers and byte values
im = Image.open("input.png") # input file
filea = open('framea.bin','wb') # field A output file
fileb = open('frameb.bin','wb') # field B output file
filepointer = fileb
mod = 128 # 256 pixel row in bytes (4bpp)
bytes = im.tobytes() # raw bitmap bytes from PNG
for i in range(len(bytes)/2):
left = ord(bytes[i*2]) # high nibble (left pixel)
right = ord(bytes[(i*2)+1]) # low nibble (right pixel)
sam = (left*16) + right # combined byte
filepointer.write(struct.pack('<B',sam)) #write to file
# switch file A/B for every display row
if i % mod == 0:
if filepointer==fileb: filepointer = filea
else: filepointer = fileb
filea.close()
fileb.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment