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
tangstad b634a9b6dc Added output of palettes.
Rotated output of data, previously it outputted by column, not row.
2005-11-09 20:29:56 +00:00

42 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])
print "elem:", newseq[-1]
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
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 "entries:", len(im.palette.palette)
for i, color in enumerate(split_seq(im.palette.palette, 3)):
r, g, b = map(lambda col : col >> 3, map(ord, color))
print "rgb:", r, g, b
value = (b << 10) + (g << 5) + r
print "color", i, ":", value
pal_output.write(struct.pack("<h", value))