-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimagelayout.py
executable file
·453 lines (402 loc) · 17.8 KB
/
imagelayout.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#!/usr/bin/env python3
'''arrange several images according to a layout'''
import yaml
import sys
import os
import argparse
from time import sleep
from PIL import Image, ImageFont, ImageDraw, ImageChops
from collections import deque, OrderedDict
import math
def autocrop(im):
'''autocrop image based on color of top left pixel'''
bg = Image.new(im.mode, im.size, im.getpixel((0,0)))
diff = ImageChops.difference(im, bg)
diff = ImageChops.add(diff, diff, 2.0, -100)
bbox = diff.getbbox()
if bbox:
return im.crop(bbox)
else:
return im
def addborder(im, size=5, color='white'):
'''add border to image with given size and color'''
if type(size) == list:
if len(size) == 2: # left-right and top-bottom borders are given
left, top = size
right, bottom = size
elif len(size) == 4: # all four borders separately given
left, top, right, bottom = size
else: # uniform border size given
top = left = bottom = right = size
if top == left == bottom == right == 0:
return im
w, h = im.size
newsize = (w+left+right, h+top+bottom)
tim = Image.new('RGBA', newsize, color)
tim.paste(im, (left, top))
return tim
def getconf(configfile):
'''load configuration file into dictionary'''
f = open(configfile)
conf = yaml.load(f, Loader=yaml.Loader)
f.close()
return conf
def imagelayout(conf, reportsizes=False, imagefiles=[], outputfile=''):
'''generate layout from given configuration'''
# initialize variables
wd = conf.get('inputdir', os.getcwd())
# set up image list
imgdata = conf['images']
for imgid in imgdata:
if type(imgdata[imgid]) == str: # only filename is given
imgdata[imgid] = {'file':imgdata[imgid]}
if imgdata[imgid]['file'][0] == '$': # file name is taken from command line argument
imgdata[imgid]['file'] = imagefiles[int(imgdata[imgid]['file'][1:])-1]
if imgdata[imgid]['file'].startswith('BLANK-'): # use a blank image
w, h = [int(s) for s in imgdata[imgid]['file'].split('-')[1].split('x')]
im = Image.new('RGBA', (w, h), color=conf.get('paddingcolor', 'white'))
if 'label' not in imgdata[imgid]:
imgdata[imgid]['label'] = {'text': ''}
elif 'text' not in imgdata[imgid]['label']:
imgdata[imgid]['label']['text'] = ''
else:
im = Image.open(os.path.join(wd, imgdata[imgid]['file']))
imgdata[imgid]['size'] = (0, 0, *im.size)
imgdata[imgid]['parts'] = [imgid]
imgdata[imgid]['img'] = im
if reportsizes: # -s option is given, report sizes and exit
for imgid in imgdata:
w, h = imgdata[imgid]['size'][2:]
print('%s %s: %dx%d aspect=%f' % (imgid, imgdata[imgid]['file'], w, h, w/h))
sys.exit()
# autocrop, crop images if requested
if conf.get('autocrop', False):
for imgid in imgdata:
if not imgdata[imgid].get('autocrop', True):
continue # do not autocrop this image
imb = autocrop(imgdata[imgid]['img'])
imgdata[imgid]['img'] = imb
imgdata[imgid]['size'] = (0, 0, *imb.size)
for imgid in imgdata:
if imgdata[imgid].get('autocrop'):
imb = autocrop(imgdata[imgid]['img'])
imgdata[imgid]['img'] = imb
imgdata[imgid]['size'] = (0, 0, *imb.size)
if 'crop' in imgdata[imgid]: # cropping defined for individual image
size = imgdata[imgid]['crop']
if type(size) == list:
if len(size) == 2:
left, top = size
right, bottom = size
elif len(size) == 4:
left, top, right, bottom = size
else:
top = left = bottom = right = size
if top == left == bottom == right == 0:
continue
w, h = imgdata[imgid]['size'][2:]
x1, y1 = left, top
x2, y2 = w-right, h-bottom
im = imgdata[imgid]['img']
imb = im.crop((x1, y1, x2, y2))
imgdata[imgid]['img'] = imb
imgdata[imgid]['size'] = (0, 0, *imb.size)
# add borders to images if requested
bordersize = 0
bordercolor = 'white'
if 'border' in conf:
bordersize = conf['border'].get('size', 10)
bordercolor = conf['border'].get('color', 'white')
for imgid in imgdata:
bs = bordersize
bc = bordercolor
if 'border' in imgdata[imgid]: # border defined for individual image
bs = imgdata[imgid]['border'].get('size', bordersize)
bc = imgdata[imgid]['border'].get('color', bordercolor)
im = imgdata[imgid]['img']
imb = addborder(im, bs, bc)
imgdata[imgid]['img'] = imb
imgdata[imgid]['size'] = (0, 0, *imb.size)
# determine final sizes
if 'layout' not in conf:
if len(imgdata) == 1:
conf['layout'] = {'hjoin': list(imgdata)}
else:
raise ValueError('layout not specified')
layout = conf['layout']
# topological sorting of directed graph defining the layout
Q = deque()
Q.appendleft(layout)
S = [layout]
c = 1
while Q:
v = Q.pop()
if type(v) == dict:
jointype, joinlist = list(v.items())[0]
jointypec = jointype+str(c)
v[jointypec] = v.pop(jointype)
c += 1
for w in v[jointypec]:
if type(w) == dict and w not in S:
S.append(w)
Q.appendleft(w)
adj = OrderedDict() # stores hjoin and vjoin lists
for dic in S[::-1]:
name = list(dic)[0]
ilist = []
for e in dic[name]:
if type(e) == dict:
ilist.append(list(e)[0])
else:
ilist.append(e)
adj[name] = ilist
# determine positions and sizes in final image
for iid in imgdata:
if 'fixedsize' not in imgdata[iid]:
imgdata[iid]['fixedsize'] = False
# do the joinings
for name in adj:
imgdata[name] = {'fixedsize': False}
tomerge = adj[name]
if True in [imgdata[iid]['fixedsize'] for iid in tomerge]:
imgdata[name]['fixedsize'] = True
widths = [imgdata[iid]['size'][2]-imgdata[iid]['size'][0] for iid in tomerge]
heights = [imgdata[iid]['size'][3]-imgdata[iid]['size'][1] for iid in tomerge]
fixs = [imgdata[iid]['fixedsize'] for iid in tomerge]
# find largest height/width image, others will be aligned to this
if name.startswith('vjoin'):
im0 = widths.index(max(widths))
elif name.startswith('hjoin'):
im0 = heights.index(max(heights))
# generate vertical join
if name.startswith('vjoin'):
wfac = [widths[im0]/widths[j] for j in range(len(widths))]
hfac = [1 if fixs[j] else wfac[j] for j in range(len(widths))]
newheights = [hfac[j]*heights[j] for j in range(len(widths))]
toth = sum(newheights)
totw = widths[im0]
for j in range(len(tomerge)):
kx, ky, kw, kh = imgdata[tomerge[j]]['size']
jw = kw if fixs[j] else widths[im0]
jh = newheights[j]
jx = widths[im0]/2-kw/2 if fixs[j] else 0
jy = sum(newheights[:j])
imgdata[tomerge[j]]['size'] = (jx, jy, jw, jh)
for part in imgdata[tomerge[j]]['parts']:
if part == tomerge[j]:
continue
kx, ky, kw, kh = imgdata[part]['size']
if fixs[j]:
imgdata[part]['size'] = (jx+kx, jy+ky, kw, kh)
else:
imgdata[part]['size'] = (wfac[j]*kx, jy+wfac[j]*ky, wfac[j]*kw, wfac[j]*kh)
# generate horizontal join
elif name.startswith('hjoin'):
hfac = [heights[im0]/heights[j] for j in range(len(heights))]
wfac = [1 if fixs[j] else hfac[j] for j in range(len(heights))]
newwidths = [wfac[j]*widths[j] for j in range(len(heights))]
toth = heights[im0]
totw = sum(newwidths)
for j in range(len(tomerge)):
kx, ky, kw, kh = imgdata[tomerge[j]]['size']
jw = newwidths[j]
jh = kh if fixs[j] else heights[im0]
jx = sum(newwidths[:j])
jy = heights[im0]/2-kh/2 if fixs[j] else 0
imgdata[tomerge[j]]['size'] = (jx, jy, jw, jh)
for part in imgdata[tomerge[j]]['parts']:
if part == tomerge[j]:
continue
kx, ky, kw, kh = imgdata[part]['size']
if fixs[j]:
imgdata[part]['size'] = (jx+kx, jy+ky, kw, kh)
else:
imgdata[part]['size'] = (jx+hfac[j]*kx, hfac[j]*ky, hfac[j]*kw, hfac[j]*kh)
# set size and parts for this join
imgdata[name]['size'] = (0, 0, totw, toth)
imgdata[name]['parts'] = sum((imgdata[iid]['parts'] for iid in tomerge), [])
last = list(adj)[-1]
totw, toth = imgdata[last]['size'][2:]
# resize whole image if requested
eids = [iid for iid in imgdata if not iid.startswith('hjoin') and not iid.startswith('vjoin')]
if 'finalwidth' in conf or 'finalheight' in conf:
if 'finalwidth' in conf:
facw = conf['finalwidth']/totw
if 'finalheight' in conf:
fach = conf['finalheight']/toth
else:
fach = facw
else:
fach = conf['finalheight']/toth
facw = fach
for iid in eids:
[x, y, w, h] = imgdata[iid]['size']
imgdata[iid]['size'] = [facw*x, fach*y, facw*w, fach*h]
totw = facw*totw
toth = fach*toth
# convert coords and sizes to integers by sum-preserving rounding
for iid in eids:
[x, y, w, h] = imgdata[iid]['size']
imgdata[iid]['intsize'] = [round(x), round(y), round(x+w)-round(x), round(y+h)-round(y)]
totwint = round(totw)
tothint = round(toth)
# create new image and paste each input image to its place
reszmethod = conf.get('resizemethod', 'nearest')
resmeth = {'nearest':Image.NEAREST, 'bilinear':Image.BILINEAR, 'bicubic':Image.BICUBIC,
'lanczos':Image.LANCZOS}[reszmethod]
imnew = Image.new('RGBA', (totwint, tothint), color=conf.get('paddingcolor', 'white'))
for imgid in eids:
[x, y, w, h] = imgdata[imgid]['intsize']
im = imgdata[imgid]['img']
imr = im.resize((w, h), resample=resmeth)
imnew.paste(imr, (x, y))
# add labels if requested
if conf.get('labels', {'add': False}).get('add', False):
fontname = conf['labels'].get('fontname', 'FreeSans')
fontsize = conf['labels'].get('fontsize', 32)
fontcolor = conf['labels'].get('fontcolor', 'black')
defaultpos = conf['labels'].get('pos', 'top-left')
defaultoffset = conf['labels'].get('offset', [0, 0])
draw = ImageDraw.Draw(imnew)
for iid in eids:
label = imgdata[iid].get('label', iid)
if type(label) == str:
labeltext = label
fn = fontname
fs = fontsize
fc = fontcolor
pos = defaultpos
offset = defaultoffset
else:
labeltext = imgdata[iid]['label'].get('text', iid)
fn = imgdata[iid]['label'].get('fontname', fontname)
fs = imgdata[iid]['label'].get('fontsize', fontsize)
fc = imgdata[iid]['label'].get('fontcolor', fontcolor)
pos = imgdata[iid]['label'].get('pos', defaultpos)
offset = imgdata[iid]['label'].get('offset', defaultoffset)
[x, y, w, h] = imgdata[iid]['intsize']
posh, posw = pos.split('-')
offsetx, offsety = offset
imfont = ImageFont.truetype(font=fn, size=fs)
if posh+posw != 'topleft':
(textw, texth) = draw.textsize(labeltext, font=imfont)
if posh == 'center':
y = y+(h-texth)/2
elif posh == 'bottom':
y = y+h-texth
if posw == 'center':
x = x+(w-textw)/2
elif posw == 'right':
x = x+w-textw
x += offsetx
y += offsety
draw.text((x, y), labeltext, fill=fc, font=imfont)
# add title if requested
if 'title' in conf and conf['title'].get('add', True):
fontname = conf['title'].get('fontname', 'FreeSans')
fontsize = conf['title'].get('fontsize', 36)
fontcolor = conf['title'].get('fontcolor', 'black')
bgcolor = conf['title'].get('bgcolor', 'white')
height = conf['title'].get('height', int(fontsize*1.1))
w, h = imnew.size
imnew2 = Image.new('RGBA', (w, h+height), color=bgcolor)
imnew2.paste(imnew, (0, height))
draw = ImageDraw.Draw(imnew2)
font = ImageFont.truetype(font=fontname, size=fontsize)
title = conf['title']['text']
(textw, texth) = draw.textsize(title, font=font)
x = (w-textw)/2
y = (height-texth)/2
draw.text((x, y), title, fill=fontcolor, font=font)
imnew = imnew2
# add lines, if any
if 'lines' in conf:
cos30 = math.cos(15/180*math.pi)
sin30 = math.sin(15/180*math.pi)
width = conf['lines'].get('width', 3)
color = conf['lines'].get('color', 'black')
for linedic in conf['lines']['linelist']:
coords = linedic['fromto']
todraw = [(coords[2*i], coords[2*i+1]) for i in range(len(coords)//2)]
lw = linedic.get('width', width)
lc = linedic.get('color', color)
arrowsize = linedic.get('arrowsize', 0)
draw = ImageDraw.Draw(imnew)
if arrowsize > 0:
# draw an arrowhead
x1, y1, x2, y2 = coords[-4:]
vx, vy = x1-x2, y1-y2
v = math.sqrt(vx**2+vy**2)
vx, vy = arrowsize*vx/v, arrowsize*vy/v
vxL = cos30*vx-sin30*vy
vyL = sin30*vx+cos30*vy
vxR = cos30*vx+sin30*vy
vyR = -sin30*vx+cos30*vy
todraw += [(x2+vxL, y2+vyL), (x2, y2), (x2+vxR, y2+vyR), (x2, y2)]
draw.line(todraw, fill=lc, width=lw, joint='curve')
# add global labels, if any
if 'globallabels' in conf:
fontname = conf['globallabels'].get('fontname', 'FreeSans')
fontsize = conf['globallabels'].get('fontsize', 36)
fontcolor = conf['globallabels'].get('fontcolor', 'black')
for labdic in conf['globallabels']['labellist']:
fn = labdic.get('fontname', fontname)
fs = labdic.get('fontsize', fontsize)
fc = labdic.get('fontcolor', fontcolor)
al = labdic.get('align', 'left')
[x, y] = labdic.get('coords', [0, 0])
draw = ImageDraw.Draw(imnew)
font = ImageFont.truetype(font=fn, size=fs)
draw.text((x, y), labdic['text'], fill=fc, font=font, align=al)
# add global border if requested
if 'globalborder' in conf:
imnew = addborder(imnew, size=conf['globalborder'].get('size', 20),
color=conf['globalborder'].get('color', 'white'))
# save result
if conf['outputfile'][0] == '$':
conf['outputfile'] = outputfile # from command line argument
ofile = conf['outputfile'].lower()
background = Image.new('RGBA', imnew.size, color=conf.get('paddingcolor', 'white'))
imnew = Image.alpha_composite(background, imnew)
# pixelscaling as last step before saving
if 'pixelscaling' in conf:
if type(conf['pixelscaling']) == list:
xscale, yscale = conf['pixelscaling']
else:
xscale = yscale = conf['pixelscaling']
h, w = imnew.size
imnew = imnew.resize((int(xscale*h), int(yscale*w)), resample=resmeth)
if ofile.endswith('.tif') or ofile.endswith('.tiff'):
imnew.save(conf['outputfile'], compression='tiff_adobe_deflate')
else:
imnew.save(conf['outputfile'])
print('Output written to %s' % (conf['outputfile']))
## Main program
def main():
parser = argparse.ArgumentParser(description='Arrange images into a layout',
epilog='See man page at https://github.com/aszilagyi/imagelayout/docs/manu.md')
parser.add_argument('-w', '--watch', action='store_true',
help='Watch the config file and re-run upon detecting a change')
parser.add_argument('-s', action='store_true', help='report image sizes and exit')
parser.add_argument('-o', dest='outputfile', help='Output image file name (optional)')
parser.add_argument('configfile', help='config file')
parser.add_argument('imagefile', nargs='*', help='image files (optional)')
a = parser.parse_args()
imagelayout(getconf(a.configfile), reportsizes=a.s, imagefiles=a.imagefile,
outputfile=a.outputfile)
if a.watch:
print('Watching config file (%s) for changes, press Ctrl-C to quit...' % (a.configfile))
mtime = os.stat(a.configfile).st_mtime
while True:
sleep(0.5)
mt = os.stat(a.configfile).st_mtime
if mt != mtime:
print('Re-running...')
try:
imagelayout(getconf(a.configfile), reportsizes=a.s, imagefiles=a.imagefile,
outputfile=a.outputfile)
except ValueError as e:
print('ValueError:', e)
mtime = mt
if __name__ == '__main__':
main()