-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtmlPhotoGallery.py
86 lines (66 loc) · 2.61 KB
/
htmlPhotoGallery.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
#
# This script adds gallery and thumbs directories and copies lower res photos
# Outputs html in format for use on LearnMYOG.com
#
# <div class="gallery style1 small">
# <article>
# <a href="images/pulloverHoodie/1.JPG" class="image">
# <img src="images/pulloverHoodie/thumbs/1.JPG" title="" loading="lazy" />
# </a>
# </article>
# </div>
#
# Activate venv : source .venv/bin/activate
#
# Place source jpgs into a jpg_path e.g. build
# Copy output folder which includes gallery.html and directories to place in website images folder /project/build
# Flip gall_path for build photos
#
import os
from PIL import Image, ImageOps
def htmlPhotoGallery(project, jpg_path, output):
# site directories for images in gallery.html
#gall_path = "images/" + project + "build/"
gall_path = "images/" + project
thumb_path = gall_path + "thumbs/"
paths = sorted(os.listdir(jpg_path))
#generate a list of inputs, then writelines(list)
line_list = []
div = '<div id = "build_gallery" style="display:none;" class="gallery style1 small">'
line_list.append(div + '\n')
for i in paths:
if i.endswith('.jpg'):
print("working on "+i)
item = (
'\t' + '<article>'
'\n\t\t' + '<a href="' + gall_path + i + '" class="image">'
'\n\t\t' + '<img src="'+ thumb_path + i + '"' + ' title="Step ' + os.path.splitext(i)[0] + '"' + ' loading="lazy" />' + '</a>'
'\n\t' + '</article>'
)
line_list.append(item + '\n')
imageDrop(i, jpg_path, gall_path, thumb_path)
line_list.append('</div>')
hf = open(output, 'w')
hf.writelines(line_list)
hf.close()
print(output,"created.")
def imageDrop(f, jpg_path, gall_path, thumb_path):
with Image.open(jpg_path + f) as image:
if not os.path.isdir(gall_path):
os.makedirs(gall_path)
if not os.path.isdir(thumb_path):
os.makedirs(thumb_path)
fixed_image = ImageOps.exif_transpose(image)
galsize = (800,2400)
#gal = image.copy().resize(galsize)
gal = fixed_image.copy()
#thumbnail respects aspect ratio
gal.thumbnail(galsize)
thumbsize = (400,1200)
thumb = fixed_image.copy()
#thumbnail respects aspect ratio for wide images
thumb.thumbnail(thumbsize)
gal.save(gall_path + f, "JPEG", quality="web_high")
thumb.save(thumb_path + f, "JPEG", quality="web_high")
if __name__ == '__main__':
htmlPhotoGallery(project='techPouch/', jpg_path='imports/', output='gallery.html')