forked from christrotter/gootsmoji
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.py
executable file
·124 lines (96 loc) · 3.57 KB
/
build.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
#!/usr/bin/env python
"""
Build the README.md Goots grid.
usage: ./build.py
"""
import math
import os
import shutil
import subprocess
from PIL import Image
# IMAGE_SOURCE_DIR is the directory to list Goots images from.
IMAGE_SOURCE_DIR = "./goots/"
# GRID_FILENAME is the filename to save the grid image to.
GRID_FILENAME = "grid_image.png"
# THUMBNAIL_WIDTH and THUMBNAIL_HEIGHT are the dimensions to resize the geets in the grid.
THUMBNAIL_WIDTH = 128
THUMBNAIL_HEIGHT = 128
# MARGIN is the amount of margin to leave around the geets in the grid.
MARGIN = 12
# GAP is the space between geets in the grid.
GAP = 12
def update_image_grid():
"""Generate the grid for the readme."""
# Get a list of all image files in the image.
image_files = [f for f in os.listdir(IMAGE_SOURCE_DIR) if f.endswith(".png")]
image_files.sort()
# Determine the number of columns and rows for the grid.
num_columns = math.ceil(math.sqrt(len(image_files)))
num_rows = (len(image_files) + num_columns - 1) // num_columns
# Create a new image with the size of the grid.
grid_width = num_columns * (THUMBNAIL_WIDTH + GAP) + (2 * MARGIN) - GAP
grid_height = num_rows * (THUMBNAIL_HEIGHT + GAP) + (2 * MARGIN) - GAP
grid_image = Image.new("RGB", (grid_width, grid_height))
# Paste each image onto the grid.
for i, image_file in enumerate(image_files):
image_path = os.path.join(IMAGE_SOURCE_DIR, image_file)
with Image.open(image_path) as image:
image.thumbnail((THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT))
row = i // num_columns
col = i % num_columns
x = col * (THUMBNAIL_WIDTH + GAP) + MARGIN
y = row * (THUMBNAIL_HEIGHT + GAP) + MARGIN
grid_image.paste(image, (x, y))
# Save the grid image.
grid_image.save(GRID_FILENAME)
# Compress using optiping if installed.
if shutil.which("optipng"):
subprocess.run(["optipng", "-o9", GRID_FILENAME], check=True)
# PICKER_INDEX is the filename of the goots web picker.
PICKER_INDEX = "picker/index.html"
# IMG_TEMPLATE is the <img> tag format string. Template parameters:
# src: The URL encoded relative path to the image.
# name: The slackmoji name (without colons).
IMG_TEMPLATE = '<img src="{src}" alt=":{name}:" title=":{name}:">'
# FILE_TEMPLATE is the format table HTML to put in the file. Template parameters:
# images: String containing all the <img> tags for each goots.
FILE_TEMPLATE = (
"<!DOCTYPE html>\n"
"<!-- THIS FILE AUTOMATICALLY GENERATED BY ../build.py -->\n"
'<html lang="en">\n'
"<head>\n"
' <link rel="stylesheet" href="style.css">\n'
' <script src="script.js" defer></script>\n'
"\n"
" <title>Goots Gallery</title>\n"
' <meta name="description" content="The ultimate goots viewing experience.">\n'
"</head>\n"
"<body>\n"
"<main>\n"
"\n"
"{images}\n"
"\n"
"</main>\n"
"</body>\n"
"</html>\n"
)
def update_picker():
"""Update the web picker."""
image_files = [f for f in os.listdir(IMAGE_SOURCE_DIR)]
image_files.sort()
rel_goots_path = os.path.relpath(os.path.join("../", IMAGE_SOURCE_DIR))
img_tags = (
IMG_TEMPLATE.format(
src=os.path.join(rel_goots_path, filename),
name=os.path.splitext(filename)[0],
)
for filename in image_files
)
with open(PICKER_INDEX, "w", encoding="utf8") as file:
file.write(FILE_TEMPLATE.format(images="\n".join(img_tags)))
def main():
"""Start of execution."""
update_image_grid()
update_picker()
if __name__ == "__main__":
main()