Skip to content

Instantly share code, notes, and snippets.

Created June 1, 2023 23:02
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
SAM Coupé PNG bitmap converter
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 ="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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment