/
photowall.py
executable file
·524 lines (395 loc) · 14.7 KB
/
photowall.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
#!/usr/bin/env python
# GNU GENERAL PUBLIC LICENSE
# Version 3, 29 June 2007
#
# Copyright (C) 2012,2016 Kevin Pouget
import os
import tempfile
import pipes
import subprocess
import time
import random
import shutil
try:
from wand.image import Image
from wand.display import display
except ImportError as e:
# cd /usr/lib/
# ln -s libMagickWand-6.Q16.so libMagickWand.so
print("Couldn't import Wand package.")
print("Please refer to #http://dahlia.kr/wand/ to install it.")
import traceback; traceback.print_exc()
raise e
try:
import magic
mime = magic.Magic()
except ImportError:
mime = None
#https://github.com/ahupp/python-magic
print("WARNING: python-magic not installed, some magic won't work")
try:
from docopt import docopt
except ImportError:
print("Couldn't import Docopt package.")
print("Please refer to#https://github.com/docopt/docopt to install it.")
print("/!\\ Option parsing not possible, defaulting to hardcoded values/!\\")
def to_bool(val):
if val is None:
return false
return val == 1
def to_int(val):
return int(val)
def to_str(val):
return val
def to_path(val):
return val
OPT_TO_KEY = {
'--do-wrap' : ("DO_WRAP", to_bool),
'--line-height': ("LINE_HEIGHT", to_int),
'--nb-lines' : ('LINES', to_int),
'--no-caption' : ("WANT_NO_CAPTION", to_bool),
'--pick-random': ("PICK_RANDOM", to_bool),
'--put-random' : ("PUT_RANDOM", to_bool),
'--no-resize' : ("DO_NOT_RESIZE", to_bool),
'--sleep' : ('SLEEP_TIME', to_int),
'--width' : ('WIDTH', to_int),
'<path>' : ('PATH', to_path),
'<target>' : ('TARGET', to_path),
'--polaroid' : ("DO_POLAROID", to_bool),
'--format' : ("IMG_FORMAT_SUFFIX", to_str),
'--crop-size' : ("CROP_SIZE", to_int),
'--help' : ("HELP", to_bool)
}
KEY_TO_OPT = dict([(key, (opt, ttype)) for opt, (key, ttype) in OPT_TO_KEY.items()])
PARAMS = {
"PATH" : "/home/kevin/mount/téléphone",
"TARGET" : "/tmp/final.png",
#define the size of the picture
"WIDTH" : 1000,
#define how many lines do we want
"LINES": None,
"LINE_HEIGHT": 200,
#minimum width of cropped image. Below that, we black it out
#only for POLAROID
"CROP_SIZE": 100,
"IMG_FORMAT_SUFFIX": ".png",
# True if end-of-line photos are wrapped to the next line
"DO_WRAP": False,
# True if we want a black background and white frame, plus details
"DO_POLAROID": True,
"WANT_NO_CAPTION": True,
# False if we want to add pictures randomly
"PUT_RANDOM": False,
"DO_NOT_RESIZE": False,
### Directory options ###
# False if we pick directory images sequentially, false if we take them randomly
"PICK_RANDOM": False,
## Random wall options ##
"SLEEP_TIME": 0,
"HELP": False
}
DEFAULTS = dict([(key, value) for key, value in PARAMS.items()])
DEFAULTS_docstr = dict([(KEY_TO_OPT[key][0], value) for key, value in PARAMS.items()])
usage = """Photo Wall generator.
Usage:
photowall.py <path> <target> [options]
Arguments:
<path> The path where photos are picked up from. [default: %(<path>)s]
<target> The path where the target photo is written. Except in POLAROID+RANDOM mode, the image will be blanked out first. [default: %(<target>)s]
Options:
--polaroid Use polaroid-like images for the wall
--pick-random Pick images randomly in the <path> folder. [default: %(--pick-random)s]
--help Display this message
Size options:
--nb-lines <nb> Number on lines of the target image. [default: 0] (use all directory images)
--line-height <height> Set the height of a single image. [default: %(--line-height)d]
--width <width> Set final image width. [default: %(--width)d]
--no-resize Resize images before putting in the wall. [default: %(--no-resize)s]
--crop-size <crop> Minimum size to allow cropping an image, if it doesn't fit [default: %(--crop-size)s]
Polaroid mode options:
--no-caption Disable caption. [default: %(--no-caption)s]
--put-random Put images randomly instead of linearily. [default: %(--put-random)s]
--sleep <time> If --put-random, time (in seconds) to go asleep before adding a new image. [default: %(--sleep)d]
Collage mode options:
--do-wrap Finish images on the next line? [default: %(--do-wrap)s]
""" % DEFAULTS_docstr
class UpdateCallback:
def newExec(self):
pass
def newImage(self, row=0, col=0, filename=""):
print("%d.%d > %s" % (row, col, filename))
def updLine(self, row, tmpLine):
#print("--- %d ---" % row)
pass
def newFinal(self, name):
pass
def finished(self, name):
print("==========")
def stopRequested(self):
return False
def checkPause(self):
pass
updateCB = UpdateCallback()
if __name__ == "__main__":
arguments = docopt(usage, version="3.5-dev")
if arguments["--help"]:
print(usage)
exit()
param_args = dict([(OPT_TO_KEY[opt][0], OPT_TO_KEY[opt][1](value)) for opt, value in arguments.items()])
PARAMS = dict(PARAMS, **param_args)
###########################################
def get_file_details(filename):
try:
link = filename
try:
link = os.readlink(filename)
except OSError:
pass
link = pipes.quote(link)
names = link[link.index("/miniatures/" if not PARAMS["NO_SWITCH_TO_MINI"] else "/images"):].split("/")[2:]
theme, year, album, fname = names
return "%s (%s)" % (album, theme)
except Exception as e:
#print("Cannot get details from {}: {}".format(filename, e))
fname = get_file_details_dir(filename)
fname = fname.rpartition(".")[0]
fname = fname.replace("_", "\n")
return fname
###########################################
class GetFileDir:
def __init__(self, randomize):
self.idx = 0
self.files = os.listdir(PARAMS["PATH"])
if len(self.files) == 0:
raise EnvironmentError("No file available")
self.files.sort()
if randomize:
random.shuffle(self.files)
def get_next_file(self):
try:
to_return = self.files[self.idx]
except IndexError: # self.idx > len(self.files)
return None
self.idx += 1
# if nb line explicitely set, go round
if PARAMS["LINES"]:
self.idx %= len(self.files)
return PARAMS["PATH"] + to_return
def get_file_details_dir(filename):
return filename[filename.rindex("/")+1:]
###########################################
###########################################
def do_append(first, second, underneath=False):
sign = "-" if underneath else "+"
background = "-background black" if PARAMS["DO_POLAROID"] else ""
command = "convert -gravity center %s %sappend %s %s %s" % (background, sign, first, second, first)
ret = subprocess.call(command, shell=True)
if ret != 0:
raise Exception("Command failed: ", command)
def do_polaroid (image, filename=None, background="black", suffix=None):
if suffix is None:
suffix = PARAMS["IMG_FORMAT_SUFFIX"]
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=suffix)
tmp.close()
print("Saving image into {}...".format(tmp.name))
image.save(filename=tmp.name)
print("Done")
if not(PARAMS["WANT_NO_CAPTION"]) and filename:
details = get_file_details(filename)
caption = """-caption "%s" """ % details.replace("'", "\\'")
else:
caption = ""
command = ("convert "
"-bordercolor snow "+
"-background %(bg)s "+
"-gravity center %(caption)s "+
"+polaroid %(name)s %(name)s") % {"bg" : background, "name":tmp.name, "caption":caption}
ret = subprocess.call(command, shell=True)
if ret != 0:
raise Exception("Command failed: "+ command)
img = Image(filename=tmp.name).clone()
os.unlink(tmp.name)
img.resize(width=image.width, height=image.height)
return img
def do_blank_image(height, width, filename, color="black"):
print("Create blank ({}) image {}*{} --> {}".format(color, width, height, filename))
command = "convert -size %dx%d xc:%s %s" % (width, height, color, filename)
ret = subprocess.call(command, shell=True)
if ret != 0:
raise Exception("Command failed: "+ command)
def do_polaroid_and_random_composite(target_filename, target, image, filename):
PERCENT_IN = 100
image = do_polaroid(image, filename, background="transparent", suffix=".png")
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=PARAMS["IMG_FORMAT_SUFFIX"])
image.save(filename=tmp.name)
height = random.randint(0, int(target.height- image.height))
width = random.randint(0, int(target.width - image.width))
geometry = "{}{}{}{}".format("+" if width >= 0 else "", width, "+" if height >= 0 else "", height)
print("{}".format(geometry))
command = "composite -geometry %s -compose Over %s %s %s" % (geometry, tmp.name, target_filename, target_filename)
ret = os.system(command)
os.unlink(tmp.name)
if ret != 0:
raise Exception("failed")
def photowall(name):
output_final = None
previous_filename = None
#for all the rows,
row = -1
done = False
while not done:
row += 1
if PARAMS["LINES"] and row >= PARAMS["LINES"]: break
output_row = None
row_width = 0
img_count = 0
#concatenate until the image width is reached
while not done and row_width < PARAMS["WIDTH"]:
# get a new file, or the end of the previous one, if it was split
filename = get_next_file() if previous_filename is None else previous_filename
if filename is None:
print("All photos have been consumed.")
done = True
continue
mimetype = None
previous_filename = None
# get a real image
if mime is not None:
mimetype = mime.from_file(filename)
if "symbolic link" in str(mimetype):
filename = os.readlink(filename)
mimetype = mime.from_file(filename)
if not "image" in str(mimetype):
continue
else:
try:
filename = os.readlink(filename)
except OSError:
pass
updateCB.newImage(row, img_count, filename)
img_count += 1
# resize the image
image = Image(filename=filename)
with image.clone() as clone:
factor = float(PARAMS["LINE_HEIGHT"]) / clone.height
print("Resize image {} of a factor {:.2f}".format(filename, factor))
clone.resize(height=PARAMS["LINE_HEIGHT"], width=int(clone.width*factor))
#if the new image makes an overflow
if row_width + clone.width > PARAMS["WIDTH"]:
print("Doesn't fit.")
#compute how many pixels will overflow
overflow = row_width + clone.width - PARAMS["WIDTH"]
will_fit = clone.width - overflow
if will_fit < PARAMS["CROP_SIZE"]:
row_width = PARAMS["WIDTH"]
previous_filename = filename
continue
if PARAMS["DO_WRAP"]:
with clone.clone() as next_img:
next_img.crop(will_fit+1, 0, width=overflow, height=PARAMS["LINE_HEIGHT"])
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=PARAMS["IMG_FORMAT_SUFFIX"])
tmp.close()
next_img.save(filename=tmp.name)
previous_filename = tmp.name
clone.crop(0, 0, width=will_fit, height=PARAMS["LINE_HEIGHT"])
if PARAMS["DO_POLAROID"]:
clone = do_polaroid(clone, filename)
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=PARAMS["IMG_FORMAT_SUFFIX"])
tmp.close()
clone.save(filename=tmp.name)
row_width += clone.width
if output_row is not None:
do_append(output_row.name, tmp.name)
os.unlink(tmp.name)
else:
output_row = tmp
updateCB.updLine(row, output_row.name)
updateCB.checkPause()
if updateCB.stopRequested():
break
else:
if output_final is not None:
do_append(output_final.name, output_row.name, underneath=True)
os.unlink(output_row.name)
else:
output_final = output_row
updateCB.newFinal(output_final.name)
if output_final is not None:
shutil.move(output_final.name, name)
updateCB.finished(name)
else:
updateCB.finished(None)
return name
def random_wall(real_target_filename):
name = real_target_filename
filename = name.rpartition("/")[-1]
name, _, ext = filename.rpartition(".")
target_filename = "{}/{}.2.{}".format(tempfile.gettempdir(), name, ext)
try:
#remove any existing tmp file
os.unlink(target_filename)
except OSError:
pass
if os.path.exists(target_filename):
#if source already exist, build up on it
os.system("cp %s %s" % (target_filename, real_target_filename))
print("Target file is %s" % real_target_filename )
target = None
if mime is not None:
try:
mimetype = mime.from_file(target_filename)
if "symbolic link" in mimetype:
filename = os.readlink(target_filename)
mimetype = mime.from_file(target_filename)
if "image" in mimetype:
target = Image(filename=target_filename)
except IOError:
pass
if target is None:
height = PARAMS["LINES"] * PARAMS["LINE_HEIGHT"]
do_blank_image(height, PARAMS["WIDTH"], target_filename)
target = Image(filename=target_filename)
cnt = 0
while True:
updateCB.checkPause()
if updateCB.stopRequested():
break
filename = get_next_file()
print(filename)
img = Image(filename=filename)
with img.clone() as clone:
if not PARAMS["DO_NOT_RESIZE"]:
factor = float(PARAMS["LINE_HEIGHT"])/clone.height
print("Resize image {} of a factor {}".format(filename, factor))
clone.resize(width=int(clone.width*factor), height=int(clone.height*factor))
do_polaroid_and_random_composite(target_filename, target, clone, filename)
updateCB.checkPause()
if updateCB.stopRequested():
break
updateCB.newImage(row=cnt, filename=filename)
updateCB.newFinal(target_filename)
os.system("cp %s %s" % (target_filename, real_target_filename))
cnt += 1
updateCB.checkPause()
if updateCB.stopRequested():
break
time.sleep(PARAMS["SLEEP_TIME"])
updateCB.checkPause()
if updateCB.stopRequested():
break
get_next_file = None
def fix_args():
global get_next_file
if PARAMS["PATH"][-1] != "/":
PARAMS["PATH"] += "/"
get_next_file = GetFileDir(PARAMS["PICK_RANDOM"]).get_next_file
def do_main():
fix_args()
updateCB.newExec()
target = PARAMS["TARGET"]
if not(PARAMS["PUT_RANDOM"]):
photowall(target)
else:
random_wall(target)
if __name__== "__main__":
do_main()