Skip to content

Commit a57dc87

Browse files
committed
Use webp for images
1 parent 31afc37 commit a57dc87

File tree

3 files changed

+69
-23
lines changed

3 files changed

+69
-23
lines changed

html/map.html

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,21 @@
126126
}
127127

128128
function _placeHTMLimage(properties) {
129-
return `
130-
<img src="${properties.food_thumb_path }" alt="Vegan ${properties.cuisine} food at ${properties.name} in ${properties.area}, San Francisco Bay Area" class="food-image-popup">
131-
`;
129+
// Create WebP + JPEG fallback for thumbnails
130+
if (properties.food_thumb_path.endsWith('.webp')) {
131+
const jpegPath = properties.food_thumb_path.replace('.webp', '.jpg');
132+
return `
133+
<picture>
134+
<source srcset="${properties.food_thumb_path}" type="image/webp">
135+
<source srcset="${jpegPath}" type="image/jpeg">
136+
<img src="${jpegPath}" alt="Vegan ${properties.cuisine} food at ${properties.name} in ${properties.area}, San Francisco Bay Area" class="food-image-popup">
137+
</picture>
138+
`;
139+
} else {
140+
return `
141+
<img src="${properties.food_thumb_path}" alt="Vegan ${properties.cuisine} food at ${properties.name} in ${properties.area}, San Francisco Bay Area" class="food-image-popup">
142+
`;
143+
}
132144
}
133145

134146
function _placeHTMLbody(properties) {

html/place.html

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,15 @@ <h1 class="name">{{ name }}</h1>
116116
<p><b class="sgfi">Something Going For It</b></p>
117117
{% endif %}
118118
{% if food_image_path %}
119-
<img src="{{ food_image_path }}" alt="{{ alt_text }}" class="food-image">
119+
<picture>
120+
{% if food_image_path.endswith('.webp') %}
121+
<source srcset="{{ food_image_path }}" type="image/webp">
122+
<source srcset="{{ food_image_path.replace('.webp', '.jpg') }}" type="image/jpeg">
123+
<img src="{{ food_image_path.replace('.webp', '.jpg') }}" alt="{{ alt_text }}" class="food-image">
124+
{% else %}
125+
<img src="{{ food_image_path }}" alt="{{ alt_text }}" class="food-image">
126+
{% endif %}
127+
</picture>
120128
{% endif %}
121129
<h3 class="notes">Remarks</h3>
122130
{{ content }}

scripts/build.py

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,21 @@ def build_vilf() -> None:
4343
list(Path("raw/food").glob("*.jpg")), desc="processing food images"
4444
):
4545
file_name = raw_jpg.parts[-1]
46-
static_fp = Path("img/food") / file_name
47-
thumb_fp = Path("img/thumb") / file_name
48-
if not (
49-
(Path("static") / static_fp).exists() & (Path("static") / thumb_fp).exists()
50-
):
46+
file_stem = raw_jpg.stem
47+
static_fp_jpg = Path("img/food") / file_name
48+
static_fp_webp = Path("img/food") / f"{file_stem}.webp"
49+
thumb_fp_jpg = Path("img/thumb") / file_name
50+
thumb_fp_webp = Path("img/thumb") / f"{file_stem}.webp"
51+
52+
# Check if all formats exist
53+
files_exist = all([
54+
(Path("static") / static_fp_jpg).exists(),
55+
(Path("static") / static_fp_webp).exists(),
56+
(Path("static") / thumb_fp_jpg).exists(),
57+
(Path("static") / thumb_fp_webp).exists()
58+
])
59+
60+
if not files_exist:
5161
with Image.open(raw_jpg) as im:
5262
assert im.size[0] / im.size[1] <= 16 / 9
5363
im = im.convert("RGB")
@@ -65,16 +75,24 @@ def build_vilf() -> None:
6575
food_image_target_size[1] + pixels_to_crop,
6676
)
6777
im_cropped = im.crop((left, upper, right, lower))
78+
79+
# Save full-size images in both formats
80+
im_cropped.save(
81+
fp=Path("static") / static_fp_jpg, format="JPEG", quality=jpg_quality
82+
)
6883
im_cropped.save(
69-
fp=Path("static") / static_fp, format="JPEG", quality=jpg_quality
84+
fp=Path("static") / static_fp_webp, format="WEBP", quality=jpg_quality
7085
)
7186

7287
# thumbnails for images on map
7388
im_thumb = im_cropped.resize(
7489
(food_thumb_target_size[0], food_thumb_target_size[1])
7590
)
7691
im_thumb.save(
77-
fp=Path("static") / thumb_fp, format="JPEG", quality=jpg_quality
92+
fp=Path("static") / thumb_fp_jpg, format="JPEG", quality=jpg_quality
93+
)
94+
im_thumb.save(
95+
fp=Path("static") / thumb_fp_webp, format="WEBP", quality=jpg_quality
7896
)
7997

8098
shutil.copytree(Path("static"), build_dir)
@@ -216,20 +234,28 @@ def format_alt_text(meta, md):
216234
return base_alt
217235

218236
def get_fp_food_image(slug):
219-
static_fp = Path(f"img/food/{slug}.jpg")
220-
return (
221-
str(Path(f"/{static_fp}"))
222-
if (Path("static") / static_fp).exists()
223-
else None
224-
)
237+
# Prefer WebP if available, fallback to JPEG
238+
webp_fp = Path(f"img/food/{slug}.webp")
239+
jpg_fp = Path(f"img/food/{slug}.jpg")
240+
241+
if (Path("static") / webp_fp).exists():
242+
return str(Path(f"/{webp_fp}"))
243+
elif (Path("static") / jpg_fp).exists():
244+
return str(Path(f"/{jpg_fp}"))
245+
else:
246+
return None
225247

226248
def get_fp_food_thumb(slug):
227-
static_fp = Path(f"img/thumb/{slug}.jpg")
228-
return (
229-
str(Path(f"/{static_fp}"))
230-
if (Path("static") / static_fp).exists()
231-
else None
232-
)
249+
# Prefer WebP if available, fallback to JPEG
250+
webp_fp = Path(f"img/thumb/{slug}.webp")
251+
jpg_fp = Path(f"img/thumb/{slug}.jpg")
252+
253+
if (Path("static") / webp_fp).exists():
254+
return str(Path(f"/{webp_fp}"))
255+
elif (Path("static") / jpg_fp).exists():
256+
return str(Path(f"/{jpg_fp}"))
257+
else:
258+
return None
233259

234260
for place_md in Path("places").glob("*.md"):
235261
try:

0 commit comments

Comments
 (0)