This repository has been archived by the owner on Jan 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_photo_gallery.py
160 lines (138 loc) · 5.08 KB
/
generate_photo_gallery.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
import os
def generate_gallery(images_folder):
# Generate the HTML code for the photo gallery webpage
html_code = '''<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Photo Gallery</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<style>
/* Styling for the thumbnails */
.thumbnail-container {
position: relative;
width: 100%;
height: 0;
padding-bottom: 100%; /* Set a fixed aspect ratio for the thumbnails (1:1 in this example) */
}
.thumbnail {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
object-fit: cover; /* Crop the images to fit the thumbnail size while preserving aspect ratio */
border: 2px solid #ccc;
padding: 5px;
}
/* Styling for the modal carousel */
.modal .carousel-item {
max-height: 90vh;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h1 class="text-center">Photo Gallery</h1>
</div>
</div>
<div class="row">
<!-- Thumbnail grid -->
<div class="col-md-12">
<div class="row justify-content-center">'''
# Iterate over the images in the folder
index = 0
for filename in os.listdir(images_folder):
if filename.lower().endswith(".jpg") or filename.lower().endswith(".png"):
image_path = os.path.join(images_folder, filename)
thumbnail_code = f'''
<div class="col-sm-6 col-md-4 col-lg-3">
<div class="thumbnail-container">
<img class="thumbnail img-fluid" src="{image_path}" alt="" data-index="{index}">
</div>
</div>'''
html_code += thumbnail_code
index += 1
html_code += '''
</div>
</div>
</div>
</div>
<!-- Modal slideshow -->
<div class="modal fade" id="modal" tabindex="-1" role="dialog" aria-labelledby="modalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-body">
<div id="carousel" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<!-- Add carousel indicators dynamically using JavaScript -->
</ol>
<div class="carousel-inner">
<!-- Add carousel items dynamically using JavaScript -->
</div>
<a class="carousel-control-prev" href="#carousel" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carousel" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
<script>
$(document).ready(function() {
// Load images into the modal carousel
function loadCarouselImages(imagePaths) {
var carouselIndicators = $(".carousel-indicators");
var carouselItems = $(".carousel-inner");
carouselIndicators.empty();
carouselItems.empty();
for (var i = 0; i < imagePaths.length; i++) {
var imagePath = imagePaths[i];
var activeClass = i === 0 ? "active" : "";
var indicatorCode = `<li data-target="#carousel" data-slide-to="${i}" class="${activeClass}"></li>`;
var carouselItemCode = `
<div class="carousel-item ${activeClass}">
<img class="d-block w-100" src="${imagePath}" alt="Slide ${i + 1}">
</div>`;
carouselIndicators.append(indicatorCode);
carouselItems.append(carouselItemCode);
}
}
// Open modal and load images into the carousel on thumbnail click
$(".thumbnail-container").click(function() {
var imagePaths = [];
// Get all thumbnail images
var thumbnails = $(".thumbnail");
thumbnails.each(function() {
imagePaths.push($(this).attr("src"));
});
loadCarouselImages(imagePaths);
// Open the modal
$("#modal").modal("show");
// Enable carousel functionality
$("#carousel").carousel();
});
});
</script>
</body>
</html>'''
return html_code
# Set the path to the "photos" folder
images_folder = "photos"
# Generate the HTML code for the photo gallery webpage
html_code = generate_gallery(images_folder)
# Write the HTML code to a file
with open("gallery_tmp.html", "w") as file:
file.write(html_code)
print("Photo gallery webpage generated successfully!")