save full image and not cropped version
This commit is contained in:
parent
0712c86338
commit
4e0732b75a
2 changed files with 44 additions and 17 deletions
|
@ -67,7 +67,7 @@ def extract_def(infile,outdir,shred=True):
|
|||
# lm,tm - left and top margin
|
||||
_,fmt,fw,fh,w,h,lm,tm = struct.unpack("<IIIIIIii", f.read(32))
|
||||
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
|
||||
|
||||
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)
|
||||
#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()
|
||||
|
||||
#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)
|
||||
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)
|
||||
import numpy as np
|
||||
pixels = np.array(im)
|
||||
pixels[pixels > 7] = color
|
||||
im = Image.fromarray(pixels)
|
||||
imo = Image.new('P', (fw,fh))
|
||||
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
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
31
makedef.py
31
makedef.py
|
@ -117,13 +117,24 @@ def makedef(indir, outdir):
|
|||
sig = None
|
||||
# sanity checks and fill infiles dict
|
||||
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:
|
||||
continue
|
||||
t,p,bid,j,fn,fw,fh,lm,tm,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,p,bid,j,fn,fmt = m.groups()
|
||||
t,bid,j,fmt = int(t),int(bid),int(j),int(fmt)
|
||||
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':
|
||||
print "input images must have a palette"
|
||||
return False
|
||||
|
@ -146,9 +157,6 @@ def makedef(indir, outdir):
|
|||
elif fmt == 2:
|
||||
data = encode3(im)
|
||||
elif fmt == 3:
|
||||
if w < 16:
|
||||
print "width must not be less than 16 for format 3"
|
||||
return False
|
||||
data = encode3(im)
|
||||
else:
|
||||
print "unknown format: %d"%fmt
|
||||
|
@ -215,11 +223,14 @@ def makedef(indir, outdir):
|
|||
# full width and full height
|
||||
# width and height
|
||||
# left and top margin
|
||||
outf.write(struct.pack("<IIIIIIii",w*h,fmt,fw,fh,w,h,lm,tm))
|
||||
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())])
|
||||
outf.write(buf)
|
||||
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 = []
|
||||
acc = 4*h
|
||||
for d in data:
|
||||
|
@ -229,6 +240,8 @@ def makedef(indir, outdir):
|
|||
for i in data:
|
||||
outf.write(i)
|
||||
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
|
||||
if offs > ushrtmax:
|
||||
print "exceeding max ushort value: %d"%offs
|
||||
|
@ -237,6 +250,8 @@ def makedef(indir, outdir):
|
|||
for i in data:
|
||||
outf.write(i)
|
||||
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
|
||||
lineoffs = []
|
||||
acc = 0
|
||||
|
|
Loading…
Reference in a new issue