-
Notifications
You must be signed in to change notification settings - Fork 0
/
flickr_photo.py
567 lines (536 loc) · 19.8 KB
/
flickr_photo.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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
"""This class has tools to create photo books with captions in .tex format"""
import sys
import pylatex
class Album:
"""Album class for representing a collection of Photos"""
def __init__(self,albumid):
self.id = albumid
self.title = ''
self.author = ''
self.owner_id = ''
self.date = ''
self.url = ''
self.album_entries = []
class Photo:
"""Photo class with metadata for a single photo"""
def __init__(self,photoid,url,location,caption,width,height):
self.id = photoid
self.url = url
self.location = location
#self.caption = pylatex.escape_latex(caption)
self.caption = caption
self.altcaptionlist = []
self.width = width
self.height = height
if width <= height:
self.orientation = 'P'
else:
self.orientation = 'L'
self.album_title = ''
self.album_url = ''
class Page:
"""Class representing a single page with checks to see what layouts can work"""
def __init__(self,photo_max_dims,one_up=False):
caption_only = False
if one_up:
landscape_width = photo_max_dims.get('landscape_width',7.5)
landscape_height = photo_max_dims.get('landscape_height',9.0)
portrait_width = photo_max_dims.get('portrait_width',7.5)
portrait_height = photo_max_dims.get('portrait_height',9.0)
else:
landscape_width = photo_max_dims.get('landscape_width',7.5)
landscape_height = photo_max_dims.get('landscape_height',4)
portrait_width = photo_max_dims.get('portrait_width',7.5)
portrait_height = photo_max_dims.get('portrait_height',4)
self.photo_list = []
self.layout = ''
# currently hard-coded for 8.5x11
self.landscape_width = landscape_width if landscape_width is not None else 7.5
self.landscape_height = landscape_height if landscape_height is not None else 4
self.portrait_width = portrait_width if portrait_width is not None else 7.5
self.portrait_height = portrait_height if portrait_height is not None else 4
self.one_up = one_up
self.caption_only = caption_only
def add_photo(self,photo):
"""Try to add next photo to page"""
self.photo_list.append(photo)
self.layout += photo.orientation
def canfit_l(self):
"""Determine if page can fit another image in landscape orientation"""
if self.one_up:
if self.layout == '':
return True
else:
canfit_set = {'','L','P','PP'}
if self.layout in canfit_set:
return True
return False
def canfit_p(self):
"""Determine if page can fit another image in portrait orientation"""
if self.one_up:
if self.layout == '':
return True
else:
canfit_set = {'','L','LP','P','PP','PPP'}
if self.layout in canfit_set:
return True
return False
def print_landscape_line(self,thisfile,filename):
"""Print a photo inline in landscape format"""
if self.one_up:
# center is different
thisfile.write('\\begin{center}')
thisfile.write('\\includegraphics[width=' + str(self.landscape_width) + 'in,'
+ 'height=' + str(self.landscape_height) + 'in,'
+ 'keepaspectratio]{' + filename + '}\n')
thisfile.write('\\end{center}')
else:
thisfile.write('\\includegraphics[width=' + str(self.landscape_width) + 'in,'
+ 'height=' + str(self.landscape_height) + 'in,'
+ 'keepaspectratio]{' + filename + '}\n')
def print_portrait_line(self,thisfile, filename):
"""Print a photo inline in portrait format"""
if self.one_up:
# center is different
thisfile.write('\\begin{center}\n')
thisfile.write('\\includegraphics[width=' + str(self.portrait_width) + 'in,'
+ 'height=' + str(self.portrait_height) + 'in,'
+ 'keepaspectratio]{' + filename + '}\n')
thisfile.write('\\end{center}\n')
else:
thisfile.write('\\includegraphics[width=' + str(self.portrait_width) + 'in,'
+ 'height=' + str(self.portrait_height) + 'in,'
+ 'keepaspectratio]{' + filename + '}\n')
def print_caption_line(self,thisfile,text):
"""Print a caption line"""
if self.one_up:
if text:
text = pylatex.escape_latex(text)
thisfile.write('\\begin{center}\n')
thisfile.write(text +'\\\\\n')
thisfile.write('\\end{center}\n')
else:
thisfile.write('\n')
else:
if text:
text = pylatex.escape_latex(text)
thisfile.write(text +'\\\\\n')
else:
thisfile.write('\n')
# final line does not need linebreak because of pagebreak
def print_caption_line_final(self,thisfile,text):
"""Print the final caption line (special case)"""
if self.one_up:
if text:
text = pylatex.escape_latex(text)
thisfile.write('\\begin{center}\n')
thisfile.write(text +'\n')
thisfile.write('\\end{center}\n')
else:
thisfile.write('\n')
else:
if text:
text = pylatex.escape_latex(text)
thisfile.write(text +'\n')
else:
thisfile.write('\n')
def print_caption_list(self,thisfile,captionlist):
for item in captionlist[:-1]:
self.print_caption_line(thisfile,item)
self.print_caption_line_final(thisfile,captionlist[-1])
def print_ll(self,thisfile):
"""Print a page with LL orientation"""
land1 = self.photo_list[0].location
land2 = self.photo_list[1].location
capt_l1 = self.photo_list[0].caption
capt_l2 = self.photo_list[1].caption
captionlist = [capt_l1, capt_l2]
altcaptionlist = []
for item in self.photo_list:
thisaltcaptionlist = item.altcaptionlist
altcaptionlist += thisaltcaptionlist
if altcaptionlist:
captionlist = altcaptionlist
thisfile.write('\n')
if not self.caption_only:
thisfile.write('% Layout LL\n')
self.print_landscape_line(thisfile, land1)
thisfile.write('\n')
thisfile.write('\\vspace{0.25in}\n')
self.print_landscape_line(thisfile, land2)
thisfile.write('\n')
self.print_caption_list(thisfile,captionlist)
if not self.caption_only:
thisfile.write('\\pagebreak\n')
def print_l(self,thisfile):
"""Print a page with L orientation"""
land1 = self.photo_list[0].location
capt_l1 = self.photo_list[0].caption
captionlist = [capt_l1]
altcaptionlist = []
for item in self.photo_list:
thisaltcaptionlist = item.altcaptionlist
altcaptionlist += thisaltcaptionlist
if altcaptionlist:
captionlist = altcaptionlist
thisfile.write('\n')
if not self.caption_only:
thisfile.write('% Layout L\n')
self.print_landscape_line(thisfile, land1)
thisfile.write('\n')
self.print_caption_list(thisfile,captionlist)
if not self.caption_only:
thisfile.write('\\pagebreak\n')
def print_pppp(self,thisfile):
"""Print a page with PPPP orientation"""
port1 = self.photo_list[0].location
port2 = self.photo_list[1].location
port3 = self.photo_list[2].location
port4 = self.photo_list[3].location
capt_p1 = self.photo_list[0].caption
capt_p2 = self.photo_list[1].caption
capt_p3 = self.photo_list[2].caption
capt_p4 = self.photo_list[3].caption
captionlist = [capt_p1,capt_p2,capt_p3,capt_p4]
altcaptionlist = []
for item in self.photo_list:
thisaltcaptionlist = item.altcaptionlist
altcaptionlist += thisaltcaptionlist
if altcaptionlist:
captionlist = altcaptionlist
thisfile.write('\n')
if not self.caption_only:
thisfile.write('% Layout PPPP\n')
self.print_portrait_line(thisfile, port1)
self.print_portrait_line(thisfile, port2)
thisfile.write('\n')
self.print_portrait_line(thisfile, port3)
self.print_portrait_line(thisfile, port4)
thisfile.write('\n')
self.print_caption_list(thisfile,captionlist)
if not self.caption_only:
thisfile.write('\\pagebreak\n')
def print_ppp(self,thisfile):
"""Print a page with PPP orientation"""
port1 = self.photo_list[0].location
port2 = self.photo_list[1].location
port3 = self.photo_list[2].location
capt_p1 = self.photo_list[0].caption
capt_p2 = self.photo_list[1].caption
capt_p3 = self.photo_list[2].caption
captionlist = [capt_p1,capt_p2,capt_p3]
altcaptionlist = []
for item in self.photo_list:
thisaltcaptionlist = item.altcaptionlist
altcaptionlist += thisaltcaptionlist
if altcaptionlist:
captionlist = altcaptionlist
thisfile.write('\n')
if not self.caption_only:
thisfile.write('% Layout PPP\n')
self.print_portrait_line(thisfile, port1)
self.print_portrait_line(thisfile, port2)
thisfile.write('\n')
self.print_portrait_line(thisfile, port3)
thisfile.write('\n')
self.print_caption_list(thisfile,captionlist)
if not self.caption_only:
thisfile.write('\\pagebreak\n')
def print_pp(self,thisfile):
"""Print a page with PP orientation"""
port1 = self.photo_list[0].location
port2 = self.photo_list[1].location
capt_p1 = self.photo_list[0].caption
capt_p2 = self.photo_list[1].caption
captionlist = [capt_p1,capt_p2]
altcaptionlist = []
for item in self.photo_list:
thisaltcaptionlist = item.altcaptionlist
altcaptionlist += thisaltcaptionlist
if altcaptionlist:
captionlist = altcaptionlist
thisfile.write('\n')
if not self.caption_only:
thisfile.write('% Layout PP\n')
self.print_portrait_line(thisfile, port1)
self.print_portrait_line(thisfile, port2)
thisfile.write('\n')
self.print_caption_list(thisfile,captionlist)
if not self.caption_only:
thisfile.write('\\pagebreak\n')
def print_p(self,thisfile):
"""Print a page with P orientation"""
port1 = self.photo_list[0].location
capt_p1 = self.photo_list[0].caption
captionlist = [capt_p1]
altcaptionlist = []
for item in self.photo_list:
thisaltcaptionlist = item.altcaptionlist
altcaptionlist += thisaltcaptionlist
if altcaptionlist:
captionlist = altcaptionlist
thisfile.write('\n')
if not self.caption_only:
thisfile.write('% Layout P\n')
self.print_portrait_line(thisfile, port1)
thisfile.write('\n')
self.print_caption_list(thisfile,captionlist)
if not self.caption_only:
thisfile.write('\\pagebreak\n')
def print_ppl(self,thisfile):
"""Print a page with PPL orientation"""
port1 = self.photo_list[0].location
port2 = self.photo_list[1].location
land1 = self.photo_list[2].location
capt_p1 = self.photo_list[0].caption
capt_p2 = self.photo_list[1].caption
capt_l1 = self.photo_list[2].caption
captionlist = [capt_p1,capt_p2,capt_l1]
altcaptionlist = []
for item in self.photo_list:
thisaltcaptionlist = item.altcaptionlist
altcaptionlist += thisaltcaptionlist
if altcaptionlist:
captionlist = altcaptionlist
thisfile.write('\n')
if not self.caption_only:
thisfile.write('% Layout PPL\n')
self.print_portrait_line(thisfile, port1)
self.print_portrait_line(thisfile, port2)
thisfile.write('\n')
thisfile.write('\\vspace{0.25in}\n')
self.print_landscape_line(thisfile, land1)
thisfile.write('\n')
self.print_caption_list(thisfile,captionlist)
if not self.caption_only:
thisfile.write('\\pagebreak\n')
def print_lpp(self,thisfile):
"""Print a page with LPP orientation"""
land1 = self.photo_list[0].location
port1 = self.photo_list[1].location
port2 = self.photo_list[2].location
capt_l1 = self.photo_list[0].caption
capt_p1 = self.photo_list[1].caption
capt_p2 = self.photo_list[2].caption
captionlist = [capt_l1,capt_p1,capt_p2]
altcaptionlist = []
for item in self.photo_list:
thisaltcaptionlist = item.altcaptionlist
altcaptionlist += thisaltcaptionlist
if altcaptionlist:
captionlist = altcaptionlist
thisfile.write('\n')
if not self.caption_only:
thisfile.write('% Layout LPP\n')
self.print_landscape_line(thisfile, land1)
thisfile.write('\n')
thisfile.write('\\vspace{0.25in}\n')
self.print_portrait_line(thisfile, port1)
self.print_portrait_line(thisfile, port2)
thisfile.write('\n')
self.print_caption_list(thisfile,captionlist)
if not self.caption_only:
thisfile.write('\\pagebreak\n')
def print_pl(self,thisfile):
"""Print a page with PL orientation"""
port1 = self.photo_list[0].location
land1 = self.photo_list[1].location
capt_p1 = self.photo_list[0].caption
capt_l1 = self.photo_list[1].caption
captionlist = [capt_p1,capt_l1]
altcaptionlist = []
for item in self.photo_list:
thisaltcaptionlist = item.altcaptionlist
altcaptionlist += thisaltcaptionlist
if altcaptionlist:
captionlist = altcaptionlist
thisfile.write('\n')
if not self.caption_only:
thisfile.write('% Layout PL\n')
self.print_portrait_line(thisfile, port1)
thisfile.write('\n')
thisfile.write('\\vspace{0.25in}\n')
self.print_landscape_line(thisfile, land1)
thisfile.write('\n')
self.print_caption_list(thisfile,captionlist)
if not self.caption_only:
thisfile.write('\\pagebreak\n')
def print_lp(self,thisfile):
"""Print a page with LP orientation"""
land1 = self.photo_list[0].location
port1 = self.photo_list[1].location
capt_l1 = self.photo_list[0].caption
capt_p1 = self.photo_list[1].caption
captionlist = [capt_l1,capt_p1]
altcaptionlist = []
for item in self.photo_list:
thisaltcaptionlist = item.altcaptionlist
altcaptionlist += thisaltcaptionlist
if altcaptionlist:
captionlist = altcaptionlist
thisfile.write('\n')
if not self.caption_only:
thisfile.write('% Layout LP\n')
self.print_landscape_line(thisfile, land1)
thisfile.write('\n')
thisfile.write('\\vspace{0.25in}\n')
self.print_portrait_line(thisfile, port1)
thisfile.write('\n')
self.print_caption_list(thisfile,captionlist)
if not self.caption_only:
thisfile.write('\\pagebreak\n')
class Section:
"""Class representing a Book section representing a group of related Pages"""
def __init__(self):
self.page_list = []
self.thisfile = ''
self.title = ''
self.author = ''
self.date = ''
self.url = ''
self.qr = ''
self.qrdim = 5.19
self.blank_after_qr = False
self.one_up = False
self.caption_only = False
def add_page(self,page):
"""Append a page to section"""
self.page_list.append(page)
def print_section(self):
"""Print a section of book"""
if self.qr != '':
self.print_qr_page(self.thisfile,self.qr)
if self.caption_only:
for thispage in self.page_list:
if thispage.layout == 'LL':
thispage.print_ll(self.thisfile)
elif thispage.layout == 'L':
thispage.print_l(self.thisfile)
elif thispage.layout == 'PPPP':
thispage.print_pppp(self.thisfile)
elif thispage.layout == 'PPP':
thispage.print_ppp(self.thisfile)
elif thispage.layout == 'PP':
thispage.print_pp(self.thisfile)
elif thispage.layout == 'P':
thispage.print_p(self.thisfile)
elif thispage.layout == 'PPL':
thispage.print_ppl(self.thisfile)
elif thispage.layout == 'LPP':
thispage.print_lpp(self.thisfile)
elif thispage.layout == 'PL':
thispage.print_pl(self.thisfile)
elif thispage.layout == 'LP':
thispage.print_lp(self.thisfile)
else:
print('That did not match any known layouts!')
sys.exit(1)
self.thisfile.write('\\pagebreak\n')
else:
for thispage in self.page_list:
if thispage.layout == 'LL':
thispage.print_ll(self.thisfile)
elif thispage.layout == 'L':
thispage.print_l(self.thisfile)
elif thispage.layout == 'PPPP':
thispage.print_pppp(self.thisfile)
elif thispage.layout == 'PPP':
thispage.print_ppp(self.thisfile)
elif thispage.layout == 'PP':
thispage.print_pp(self.thisfile)
elif thispage.layout == 'P':
thispage.print_p(self.thisfile)
elif thispage.layout == 'PPL':
thispage.print_ppl(self.thisfile)
elif thispage.layout == 'LPP':
thispage.print_lpp(self.thisfile)
elif thispage.layout == 'PL':
thispage.print_pl(self.thisfile)
elif thispage.layout == 'LP':
thispage.print_lp(self.thisfile)
else:
print('That did not match any known layouts!')
sys.exit(1)
@staticmethod
def print_blank_page(thisfile):
"""Print a blank page"""
thisfile.write('\\newpage\n')
thisfile.write('\n')
thisfile.write('\\ % The empty page\n')
thisfile.write('\n')
thisfile.write('\\newpage\n')
def print_qr_page(self,thisfile,qr_location):
"""Print a page with a qr code"""
thisfile.write('\n')
thisfile.write('\\section*{' + self.title + '}\n\n')
thisfile.write('\\url{' + self.url + '}\n\n')
thisfile.write('Scan the QR code below to go to the original album '
+ 'with full-size photos on Flickr:\n\n')
thisfile.write('\\begin{center}\n')
thisfile.write('\\includegraphics[width=' + str(self.qrdim) + 'in]{' + qr_location + '}\n')
thisfile.write('\\end{center}\n')
thisfile.write('\\pagebreak\n')
if self.blank_after_qr:
Section.print_blank_page(thisfile)
class Book:
"""Book class representing a single photo book"""
def __init__(self,thisfile,paper_dimensions,one_up):
self.section_list = []
self.thisfile = thisfile
self.title = ''
self.author = ''
self.date = ''
self.url = ''
self.qr = ''
self.paper_dimensions = paper_dimensions
self.one_up = one_up
self.caption_only = False
def print_book(self):
"""Print book to .tex file"""
thisfile = self.thisfile
self.print_preamble(thisfile)
Book.print_begin(thisfile)
# this is dodgy
for thissection in self.section_list:
thissection.thisfile = thisfile
thissection.print_section()
Book.print_end(thisfile)
thisfile.close()
def print_preamble(self,thisfile):
"""Print preamble of latex document given margins which are currently ignored"""
if not self.one_up:
# ignore inputs for now
self.paper_dimensions = {}
top_margin = self.paper_dimensions.get('top_margin',0.75)
bottom_margin = self.paper_dimensions.get('bottom_margin',0.75)
left_margin = self.paper_dimensions.get('left_margin',0.75)
right_margin = self.paper_dimensions.get('right_margin',0.75)
paper_width = self.paper_dimensions.get('paper_width',8.5)
paper_height = self.paper_dimensions.get('paper_height',11)
thisfile.write('\\documentclass[10pt,letterpaper]{article}\n')
if self.one_up:
thisfile.write('\\pagenumbering{gobble}\n')
thisfile.write('\\usepackage[top=' + str(top_margin) + 'in,'
+ ' bottom=' + str(bottom_margin) + 'in,'
+ ' left=' + str(left_margin) + 'in,'
+ ' right=' + str(right_margin) + 'in,'
+ ' paperwidth=' + str(paper_width) + 'in,'
+ ' paperheight=' + str(paper_height) + 'in'
+ ']{geometry}\n')
thisfile.write("\\usepackage{amsfonts,amssymb,amsmath}\n")
thisfile.write("\\usepackage{pslatex}\n")
thisfile.write("\\usepackage[pdftex]{graphicx}\n")
thisfile.write("\\usepackage{float}\n")
thisfile.write("\\usepackage{hyperref}\n")
thisfile.write("\\setlength{\\parindent}{0pt}\n")
thisfile.write("\\title{" + self.title + "}\n")
thisfile.write("\\author{" + self.author + "}\n")
thisfile.write("\\date{" + self.date + "}\n")
@staticmethod
def print_begin(thisfile):
"""Print beginning of latex document"""
thisfile.write('\\begin{document}\n')
@staticmethod
def print_end(thisfile):
"""Print end of latex document"""
thisfile.write('\\end{document}\n')