save full image and not cropped version

This commit is contained in:
josch 2014-03-16 12:04:45 +01:00
parent 0712c86338
commit 4e0732b75a
2 changed files with 44 additions and 17 deletions

View file

@ -67,7 +67,7 @@ def extract_def(infile,outdir,shred=True):
# lm,tm - left and top margin # lm,tm - left and top margin
_,fmt,fw,fh,w,h,lm,tm = struct.unpack("<IIIIIIii", f.read(32)) _,fmt,fw,fh,w,h,lm,tm = struct.unpack("<IIIIIIii", f.read(32))
n = os.path.splitext(n)[0] n = os.path.splitext(n)[0]
outname = "%s"%outdir+os.sep+"%02d_%s_%02d_%02d_%s_%dx%d_%dx%d_%d.png"%(t,bn,bid,j,n,fw,fh,lm,tm,fmt) outname = "%s"%outdir+os.sep+"%02d_%s_%02d_%02d_%s_%d.png"%(t,bn,bid,j,n,fmt)
print "writing to %s"%outname print "writing to %s"%outname
if w != 0 and h != 0: if w != 0 and h != 0:
@ -143,14 +143,26 @@ def extract_def(infile,outdir,shred=True):
#tw,th = draw.textsize("%d%s"%(j,bn),font=font) #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) #draw.text(((w*3-tw)/2,(h*3-th)/2),"%d%s"%(j,bn),font=font)
#im = im.resize((w,h),Image.ANTIALIAS) #im = im.resize((w,h),Image.ANTIALIAS)
pixels = im.load()
#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
color = get_color(bn) color = get_color(bn)
for i in range(w): import numpy as np
for j in range(h): pixels = np.array(im)
if pixels[i,j] > 7: pixels[pixels > 7] = color
pixels[i,j] = color im = Image.fromarray(pixels)
im.putpalette(palette) imo = Image.new('P', (fw,fh))
im.save(outname) imo.putpalette(palette)
imo.paste(im,(lm,tm))
#draw = ImageDraw.Draw(imo)
#tw,th = draw.textsize(bn,font=font)
#draw.text(((fw-tw)/2,(fh-th)/2),bn,255,font=font)
imo.save(outname)
return True return True
if __name__ == '__main__': if __name__ == '__main__':

View file

@ -117,13 +117,24 @@ def makedef(indir, outdir):
sig = None sig = None
# 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-z0-9_]+)_(\d\d)_(\d\d)_([A-Za-z0-9_]+)_(\d+)x(\d+)_(\d+)x(\d+)_([0-3]).png', f) m = re.match('(\d+)_([a-z0-9_]+)_(\d+)_(\d+)_([A-Za-z0-9_]+)_([0-3]).png', f)
if not m: if not m:
continue continue
t,p,bid,j,fn,fw,fh,lm,tm,fmt = m.groups() t,p,bid,j,fn,fmt = m.groups()
t,bid,j,fw,fh,lm,tm,fmt = int(t),int(bid),int(j),int(fw),int(fh),int(lm),int(tm),int(fmt) t,bid,j,fmt = int(t),int(bid),int(j),int(fmt)
im = Image.open(os.sep.join([indir,f])) im = Image.open(os.sep.join([indir,f]))
w,h = im.size fw,fh = im.size
lm,tm,rm,bm = im.getbbox() or (0,0,0,0)
# format 3 has to have width and lm divisible by 32
if fmt == 3 and lm%32 != 0:
# shrink lm to the previous multiple of 32
lm = (lm/32)*32
w,h = rm-lm,bm-tm
if fmt == 3 and w%32 != 0:
# grow rm to the next multiple of 32
w = (((w-1)>>5)+1)<<5
rm = lm+w
im = im.crop((lm,tm,rm,bm))
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
@ -146,9 +157,6 @@ def makedef(indir, outdir):
elif fmt == 2: elif fmt == 2:
data = encode3(im) data = encode3(im)
elif fmt == 3: elif fmt == 3:
if w < 16:
print "width must not be less than 16 for format 3"
return False
data = encode3(im) data = encode3(im)
else: else:
print "unknown format: %d"%fmt print "unknown format: %d"%fmt
@ -215,11 +223,14 @@ def makedef(indir, outdir):
# full width and full height # full width and full height
# width and height # width and height
# left and top margin # left and top margin
outf.write(struct.pack("<IIIIIIii",w*h,fmt,fw,fh,w,h,lm,tm))
if fmt == 0: if fmt == 0:
s = len(data)
outf.write(struct.pack("<IIIIIIii",s,fmt,fw,fh,w,h,lm,tm))
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)
elif fmt == 1: elif fmt == 1:
s = 4*h+sum(len(d) for d in data)
outf.write(struct.pack("<IIIIIIii",s,fmt,fw,fh,w,h,lm,tm))
lineoffs = [] lineoffs = []
acc = 4*h acc = 4*h
for d in data: for d in data:
@ -229,6 +240,8 @@ def makedef(indir, outdir):
for i in data: for i in data:
outf.write(i) outf.write(i)
elif fmt == 2: elif fmt == 2:
s = 2+sum(len(d) for d in data)
outf.write(struct.pack("<IIIIIIii",s,fmt,fw,fh,w,h,lm,tm))
offs = outf.tell()-32+2 offs = outf.tell()-32+2
if offs > ushrtmax: if offs > ushrtmax:
print "exceeding max ushort value: %d"%offs print "exceeding max ushort value: %d"%offs
@ -237,6 +250,8 @@ def makedef(indir, outdir):
for i in data: for i in data:
outf.write(i) outf.write(i)
elif fmt == 3: elif fmt == 3:
s = (w/16)*h+sum(len(d) for d in data)
outf.write(struct.pack("<IIIIIIii",s,fmt,fw,fh,w,h,lm,tm))
# store the same value in all w/16 blocks per line # store the same value in all w/16 blocks per line
lineoffs = [] lineoffs = []
acc = 0 acc = 0