Skip to content

Instantly share code, notes, and snippets.

@foone
Last active July 16, 2018 02:53
Show Gist options
  • Save foone/0bb728e528031a1e381d8c0faab142b8 to your computer and use it in GitHub Desktop.
Save foone/0bb728e528031a1e381d8c0faab142b8 to your computer and use it in GitHub Desktop.
from PIL import Image
from struct import pack,unpack
from random import randint
import pdb
import sys
im=Image.open(sys.argv[1])
colors={}
for i in range(256):
r,g,b = unpack('3B',im.palette.palette[i*3:i*3+3])
colors[i]=(r,g,b,255)
with open('rat.a8','wb') as f:
f.write('\xA3\xA2\xA1\xA0')
f.write(pack('<LLLLL',im.size[0],im.size[1],8,0,0))
for c in range(256):
r,g,b,a=colors[c]
f.write(pack('BBBB',b,g,r,255))
for y in range(im.size[1]-1,-1,-1):
for x in range(im.size[0]):
d=im.getpixel((x,y))
f.write(chr(d))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment