tangstad/helds
tangstad
/
helds
Archived
1
0
Fork 0
This repository has been archived on 2024-07-04. You can view files and clone it, but cannot push or open issues or pull requests.
helds/gba/tools/img2bin.py

39 lines
1.1 KiB
Python

#!/usr/bin/python
from PIL import Image
import sys
import os
import struct
"""Convert image to raw binary using Python Imaging Library."""
def split_seq(seq, splitsize):
newseq = []
while seq:
newseq.append(seq[:splitsize])
seq = seq[splitsize:]
return newseq
if __name__=="__main__":
if len(sys.argv) < 3:
print "Usage: %s <source> <target> [palette]" % os.path.split(sys.argv[0])[1]
print "WARNING: It only converts 8bit images at the moment... and no tiling."
sys.exit()
im = Image.open(sys.argv[1])
output = open(sys.argv[2], "wb")
width, height = im.size
print "Converting %dx%d image to binary." % (width, height)
for y in range(height):
for x in range(width):
val = im.getpixel((x, y))
output.write(chr(val))
if len(sys.argv) > 3:
pal_output = open(sys.argv[3], "wb")
print "Converting %d color palette." % (len(im.palette.palette) / 3)
for color in split_seq(im.palette.palette, 3):
r, g, b = map(lambda col : col >> 3, map(ord, color))
value = (b << 10) + (g << 5) + r
pal_output.write(struct.pack("<h", value))