Added tiled output.
This commit is contained in:
parent
dc0f14cce8
commit
1a67a1b1f2
|
@ -3,6 +3,7 @@ from PIL import Image
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
import struct
|
import struct
|
||||||
|
from getopt import gnu_getopt
|
||||||
|
|
||||||
"""Convert image to raw binary using Python Imaging Library."""
|
"""Convert image to raw binary using Python Imaging Library."""
|
||||||
|
|
||||||
|
@ -14,22 +15,38 @@ def split_seq(seq, splitsize):
|
||||||
return newseq
|
return newseq
|
||||||
|
|
||||||
if __name__=="__main__":
|
if __name__=="__main__":
|
||||||
if len(sys.argv) < 3:
|
|
||||||
print "Usage: %s <source> <target> [palette]" % os.path.split(sys.argv[0])[1]
|
tiled, args = gnu_getopt(sys.argv[1:], "t")
|
||||||
print "WARNING: It only converts 8bit images at the moment... and no tiling."
|
|
||||||
|
if len(args) < 2:
|
||||||
|
print "Usage: %s [-t] <source> <target> [palette]" % os.path.split(sys.argv[0])[1]
|
||||||
|
print " -t - Convert data in 8x8 pixel tiles in 1D layout."
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
im = Image.open(sys.argv[1])
|
im = Image.open(args[0])
|
||||||
output = open(sys.argv[2], "wb")
|
output = open(args[1], "wb")
|
||||||
width, height = im.size
|
width, height = im.size
|
||||||
print "Converting %dx%d image to binary." % (width, height)
|
print "Converting %dx%d image to binary." % (width, height)
|
||||||
|
if not tiled:
|
||||||
for y in range(height):
|
for y in range(height):
|
||||||
for x in range(width):
|
for x in range(width):
|
||||||
val = im.getpixel((x, y))
|
val = im.getpixel((x, y))
|
||||||
output.write(chr(val))
|
output.write(chr(val))
|
||||||
|
else:
|
||||||
|
print "Doing 1D tile conversion."
|
||||||
|
if (width % 8) or (height % 8):
|
||||||
|
print "Can't tile image where width or height is not multiple of 8."
|
||||||
|
exit(-1)
|
||||||
|
else:
|
||||||
|
for offset_y in map(lambda n : 8*n, range(height / 8)):
|
||||||
|
for offset_x in map(lambda n : 8*n, range(width / 8)):
|
||||||
|
for y in range(8):
|
||||||
|
for x in range(8):
|
||||||
|
val = im.getpixel((x+offset_x, y+offset_y))
|
||||||
|
output.write(chr(val))
|
||||||
|
|
||||||
if len(sys.argv) > 3:
|
if len(args) > 2:
|
||||||
pal_output = open(sys.argv[3], "wb")
|
pal_output = open(args[2], "wb")
|
||||||
print "Converting %d color palette." % (len(im.palette.palette) / 3)
|
print "Converting %d color palette." % (len(im.palette.palette) / 3)
|
||||||
for color in split_seq(im.palette.palette, 3):
|
for color in split_seq(im.palette.palette, 3):
|
||||||
r, g, b = map(lambda col : col >> 3, map(ord, color))
|
r, g, b = map(lambda col : col >> 3, map(ord, color))
|
||||||
|
|
Reference in New Issue