Added output of palettes.
Rotated output of data, previously it outputted by column, not row.
This commit is contained in:
parent
8474f213f5
commit
b634a9b6dc
|
@ -2,19 +2,40 @@
|
|||
from PIL import Image
|
||||
import sys
|
||||
import os
|
||||
import struct
|
||||
|
||||
"""Convert image to raw binary using Python Imaging Library."""
|
||||
|
||||
if len(sys.argv) != 3:
|
||||
print "Usage: %s <source> <target>" % os.path.split(sys.argv[0])[1]
|
||||
print "WARNING: It only converts 8x8 pixel images at 8bit the moment."
|
||||
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")
|
||||
for x in range(8):
|
||||
for y in range(8):
|
||||
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))
|
||||
|
||||
|
|
Reference in New Issue