move shredding from makedef to defextract

This commit is contained in:
josch 2014-03-14 13:05:33 +01:00
parent 2a7f5da0d0
commit e0d7346b4a
3 changed files with 39 additions and 25 deletions

View file

@ -3,7 +3,14 @@ import colorsys
from PIL import ImageFont from PIL import ImageFont
font = ImageFont.truetype("/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf", 24) font = ImageFont.truetype("/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf", 34)
def sanitize_filename(fname):
# find the first character outside range [32-126]
for i,c in enumerate(fname):
if ord(c) < 32 or ord(c) > 126:
break
return fname[:i]
def get_complement(r,g,b): def get_complement(r,g,b):
r = r/255.0 r = r/255.0

View file

@ -4,18 +4,18 @@
# vcmi/client/CAnimation.cpp # vcmi/client/CAnimation.cpp
import struct import struct
from PIL import Image from PIL import Image, ImageDraw
from collections import defaultdict from collections import defaultdict
import os import os
from common import crc24_func, font, sanitize_filename
def sanitize_filename(fname): def get_color(fname):
# find the first character outside range [32-126] crc = crc24_func(fname)
for i,c in enumerate(fname): # values 0-7 must not be used as they might represent transparency
if ord(c) < 32 or ord(c) > 126: # so we are left with 248 values
break return 8+crc%248
return fname[:i]
def extract_def(infile,outdir): def extract_def(infile,outdir,shred=True):
f = open(infile) f = open(infile)
bn = os.path.basename(infile) bn = os.path.basename(infile)
bn = os.path.splitext(bn)[0] bn = os.path.splitext(bn)[0]
@ -135,6 +135,18 @@ def extract_def(infile,outdir):
h = 1 h = 1
# TODO: encode this information correctly and dont create a fake 1px image # TODO: encode this information correctly and dont create a fake 1px image
im = Image.new('P', (w,h)) im = Image.new('P', (w,h))
if shred:
#im = Image.new("P", (w*3,h*3), get_color(bn))
#draw = ImageDraw.Draw(im)
#tw,th = draw.textsize("%d%s"%(j,bn),font=font)
#draw.text(((w*3-tw)/2,(h*3-th)/2),"%d%s"%(j,bn),font=font)
#im = im.resize((w,h),Image.ANTIALIAS)
pixels = im.load()
color = get_color(bn)
for i in range(w):
for j in range(h):
if pixels[i,j] > 7:
pixels[i,j] = color
im.putpalette(palette) im.putpalette(palette)
im.save(outname) im.save(outname)
return True return True

View file

@ -4,18 +4,12 @@ import os
import re import re
import struct import struct
from collections import defaultdict from collections import defaultdict
from PIL import Image, ImageDraw from PIL import Image
from common import crc24_func, font
def get_color(fname): def makedef(indir, outdir):
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):
infiles = defaultdict(list) infiles = defaultdict(list)
sig = None sig = None
tw,th = 0,0
# sanity checks and fill infiles dict # sanity checks and fill infiles dict
for f in os.listdir(indir): 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) 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) 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])) im = Image.open(os.sep.join([indir,f]))
w,h = im.size w,h = im.size
tw,th = fw,fh
if im.mode != 'P': if im.mode != 'P':
print "input images must have a palette" print "input images must have a palette"
return False return False
@ -58,8 +53,8 @@ def makedef(indir, outdir, shred=True):
# write the header # write the header
# full width and height are not used and not the same for all frames # full width and height are not used and not the same for all frames
# in some defs, so setting to zero # in some defs, so just putting the last known value
outf.write(struct.pack("<IIII", t,0,0,len(infiles))) outf.write(struct.pack("<IIII", t,tw,th,len(infiles)))
# write the palette # write the palette
outf.write(struct.pack("768B", *pal)) outf.write(struct.pack("768B", *pal))
@ -85,12 +80,12 @@ def makedef(indir, outdir, shred=True):
for bid,l in infiles.items(): for bid,l in infiles.items():
for im,_,p,j,_,fw,fh,lm,tm in l: for im,_,p,j,_,fw,fh,lm,tm in l:
w,h = im.size 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)) 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())]) buf = ''.join([chr(i) for i in list(im.getdata())])
outf.write(buf) outf.write(buf)
return True return True