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
|
#!/usr/bin/env python3
# Copyright 2017 vg@devys.org
# SPDX-License-Identifier: MIT
'''
Make a html gallery of given directory. Supports image and videos.
Usage: mkgallery.py [options] [--] DIRECTORY
mkgallery.py -h|--help
Options:
-h, --help Display this help message
-g GALLERY Where to generate gallery
'''
import os
import urllib
import math
import docopt
import cv2
class Gallery:
'Gallery generator class'
def __init__(self, args):
self.gallery_dir = args.get('GALLERY', 'gallery')
self.source_dir = args['DIRECTORY']
def generate_thumbnail(self, original_filename):
'generate a thumbnail of original_filename in a gallery subdir'
#sizes = [(120, 120), (720, 720), (1600, 1600)]
#image = cv2.imread("input.jpg")
#for size in sizes:
# resized_image = cv2.resize(image, size)
# cv2.imwrite("thumbnail_%d.jpg" % size[0], resized_image)
image = cv2.imread(original_filename)
sizes = [(128, 128)]
for size in sizes:
p = os.path
thumbnail_dir = p.join(self.gallery_dir,
p.dirname(original_filename))
os.makedirs(thumbnail_dir, exist_ok=True)
thumbnail_basename = '%s_%dx%d.jpg' % (p.splitext(p.basename(original_filename))[0], size[0], size[1])
thumbnail_filename = p.join(thumbnail_dir, thumbnail_basename)
image_height, image_width = image.shape[:2]
aspect_ratio = image_width / image_height
area = size[0] * size[1]
height = math.sqrt(area / aspect_ratio)
width = height * aspect_ratio
aspect_ratio_size = (int(width), int(height))
print('debug thumbnail size:', aspect_ratio_size)
resized_image = cv2.resize(image, aspect_ratio_size, cv2.INTER_AREA)
cv2.imwrite(thumbnail_filename, resized_image)
return thumbnail_filename
def generate_entry(self, filename):
'Generate html code for specific image source filename.'
thumbnail_filename = self.generate_thumbnail(filename)
return '''\
<p><a href="{encoded_filename}"><img src="{thumbnail_filename}"></a></p>
'''.format(
encoded_filename=urllib.parse.quote(os.path.abspath(filename)),
thumbnail_filename=urllib.parse.quote(thumbnail_filename[len(self.gallery_dir):].lstrip('/')),
)
def content_generator(self):
'generate <img> entry for each source image'
for dirpath, _, filenames in os.walk(self.source_dir):
for filename in filenames:
yield self.generate_entry(os.path.join(dirpath, filename))
def generate_html_file(self, output_stream):
'generate a single html file'
output_stream.write('''\
<html>
<head>
<title>Python gallery</title>
</head>
<body>
''')
for content in self.content_generator():
output_stream.write(content)
output_stream.write('''\
</body>
</html>
''')
def mkgallery(self):
'global call for generating a gallery'
os.makedirs(self.gallery_dir, exist_ok=True)
filename = os.path.join(self.gallery_dir, 'index.html')
with open(filename, 'w', encoding='utf8') as outstream:
self.generate_html_file(outstream)
def main():
'function called only when script invoked directly on command line'
args = docopt.docopt(__doc__)
gallery = Gallery(args)
gallery.mkgallery()
if __name__ == '__main__':
main()
|