Downsize thumbnails to 1x/2x to reduce page weight
authorAlex Chan <alex@alexwlchan.net>
Tue, 4 Feb 2020 21:27:27 +0000 (21:27 +0000)
committerAlex Chan <alex@alexwlchan.net>
Tue, 4 Feb 2020 21:27:27 +0000 (21:27 +0000)
scripts/render_html.py
templates/_book_cover.html

index c55d581..9f077f3 100755 (executable)
@@ -165,23 +165,35 @@ def render_individual_review(env, *, review_entry):
 
 
 def _create_new_thumbnail(src_path, dst_path):
+    dst_path.parent.mkdir(exist_ok=True)
+
     im = Image.open(src_path)
-    im.thumbnail((240, 240))
-    os.makedirs(os.path.dirname(dst_path), exist_ok=True)
+
+    if im.width > 240 and im.height > 240:
+        im.thumbnail((240, 240))
     im.save(dst_path)
 
+    if im.width > 120 and im.height > 120:
+        im.thumbnail((120, 120))
+    im.save(dst_path.with_name(thumbnail_1x(name)))
+
+
+def thumbnail_1x(name):
+    pth = pathlib.Path(name)
+    return pth.stem + "_1x" + pth.suffix
+
 
 def create_thumbnails():
     for image_name in os.listdir("src/covers"):
         if image_name == ".DS_Store":
             continue
 
-        src_path = os.path.join("src/covers", image_name)
-        dst_path = os.path.join("_html/thumbnails", image_name)
+        src_path = pathlib.Path("src/covers") / image_name
+        dst_path = pathlib.Path("_html/thumbnails") / image_name
 
-        if not os.path.exists(dst_path):
+        if not dst_path.exists():
             _create_new_thumbnail(src_path, dst_path)
-        elif os.stat(src_path).st_mtime > os.stat(dst_path).st_mtime:
+        elif src_path.stat().st_mtime > dst_path.stat().st_mtime:
             _create_new_thumbnail(src_path, dst_path)
 
 
@@ -194,6 +206,7 @@ def main():
     env.filters["render_markdown"] = render_markdown
     env.filters["render_date"] = render_date
     env.filters["smartypants"] = smartypants.smartypants
+    env.filters["thumbnail_1x"] = thumbnail_1x
 
     create_thumbnails()
 
index c0a672b..d6abbe3 100644 (file)
@@ -1 +1 @@
-<img src="/thumbnails/{{ book.cover_image }}" alt="Cover of {{ book.title }}. {{ book.cover_desc }}">
+<img src="/thumbnails/{{ book.cover_image }}" srcset="/thumbnails/{{ book.cover_image }} 2x, /thumbnails/{{ book.cover_image|thumbnail_1x }} 1x" alt="Cover of {{ book.title }}. {{ book.cover_desc }}">