SAM Coupé PNG bitmap converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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