|
|
|
@ -4,18 +4,12 @@ import os
|
|
|
|
|
import re
|
|
|
|
|
import struct
|
|
|
|
|
from collections import defaultdict
|
|
|
|
|
from PIL import Image, ImageDraw
|
|
|
|
|
from common import crc24_func, font
|
|
|
|
|
from PIL import Image
|
|
|
|
|
|
|
|
|
|
def get_color(fname):
|
|
|
|
|
crc = crc24_func(fname)
|
|
|
|
|
# values 0-7 must not be used as they might represent transparency
|
|
|
|
|
# so we are left with 248 values
|
|
|
|
|
return 8+crc%248
|
|
|
|
|
|
|
|
|
|
def makedef(indir, outdir, shred=True):
|
|
|
|
|
def makedef(indir, outdir):
|
|
|
|
|
infiles = defaultdict(list)
|
|
|
|
|
sig = None
|
|
|
|
|
tw,th = 0,0
|
|
|
|
|
# sanity checks and fill infiles dict
|
|
|
|
|
for f in os.listdir(indir):
|
|
|
|
|
m = re.match('(\d+)_([A-Za-z0-9_]+)_(\d\d)_(\d\d)_([A-Za-z0-9_]+)_(\d+)x(\d+)_(\d+)x(\d+).png', f)
|
|
|
|
@ -25,6 +19,7 @@ def makedef(indir, outdir, shred=True):
|
|
|
|
|
t,bid,j,fw,fh,lm,tm = int(t),int(bid),int(j),int(fw),int(fh),int(lm),int(tm)
|
|
|
|
|
im = Image.open(os.sep.join([indir,f]))
|
|
|
|
|
w,h = im.size
|
|
|
|
|
tw,th = fw,fh
|
|
|
|
|
if im.mode != 'P':
|
|
|
|
|
print "input images must have a palette"
|
|
|
|
|
return False
|
|
|
|
@ -58,8 +53,8 @@ def makedef(indir, outdir, shred=True):
|
|
|
|
|
|
|
|
|
|
# write the header
|
|
|
|
|
# full width and height are not used and not the same for all frames
|
|
|
|
|
# in some defs, so setting to zero
|
|
|
|
|
outf.write(struct.pack("<IIII", t,0,0,len(infiles)))
|
|
|
|
|
# in some defs, so just putting the last known value
|
|
|
|
|
outf.write(struct.pack("<IIII", t,tw,th,len(infiles)))
|
|
|
|
|
# write the palette
|
|
|
|
|
outf.write(struct.pack("768B", *pal))
|
|
|
|
|
|
|
|
|
@ -85,12 +80,12 @@ def makedef(indir, outdir, shred=True):
|
|
|
|
|
for bid,l in infiles.items():
|
|
|
|
|
for im,_,p,j,_,fw,fh,lm,tm in l:
|
|
|
|
|
w,h = im.size
|
|
|
|
|
# size
|
|
|
|
|
# format = 0 (uncompressed)
|
|
|
|
|
# full width and full height
|
|
|
|
|
# width and height
|
|
|
|
|
# left and top margin
|
|
|
|
|
outf.write(struct.pack("<IIIIIIii",w*h,0,fw,fh,w,h,lm,tm))
|
|
|
|
|
if shred:
|
|
|
|
|
im = Image.new("P", (w*3,h*3), get_color(p))
|
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
|
draw.text((0,0),"%d%s"%(j,p),font=font)
|
|
|
|
|
im = im.resize((w,h),Image.ANTIALIAS)
|
|
|
|
|
buf = ''.join([chr(i) for i in list(im.getdata())])
|
|
|
|
|
outf.write(buf)
|
|
|
|
|
return True
|
|
|
|
|