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
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):
r = r/255.0

View file

@ -4,18 +4,18 @@
# vcmi/client/CAnimation.cpp
import struct
from PIL import Image
from PIL import Image, ImageDraw
from collections import defaultdict
import os
from common import crc24_func, font, sanitize_filename
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_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 extract_def(infile,outdir):
def extract_def(infile,outdir,shred=True):
f = open(infile)
bn = os.path.basename(infile)
bn = os.path.splitext(bn)[0]
@ -135,6 +135,18 @@ def extract_def(infile,outdir):
h = 1
# TODO: encode this information correctly and dont create a fake 1px image
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.save(outname)
return True

View file

@ -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