Downsize images in the thumbnail view
authorAlex Chan <alex@alexwlchan.net>
Tue, 4 Feb 2020 18:10:34 +0000 (18:10 +0000)
committerAlex Chan <alex@alexwlchan.net>
Tue, 4 Feb 2020 18:10:34 +0000 (18:10 +0000)
On the reviews page, this cuts the page weight from 4.5 MB to ~850 KB.

requirements.in
requirements.txt
scripts/deploy.sh
scripts/render_html.py
templates/_book_cover.html

index 376c7c5..27dc692 100644 (file)
@@ -3,6 +3,7 @@ hyperlink
 inquirer
 jinja2
 Markdown
 inquirer
 jinja2
 Markdown
+Pillow
 python-frontmatter
 smartypants
 unidecode
 python-frontmatter
 smartypants
 unidecode
index bc0e278..5a6bd38 100644 (file)
@@ -12,6 +12,7 @@ inquirer==2.6.3
 jinja2==2.11.1
 markdown==3.1.1
 markupsafe==1.1.1         # via jinja2
 jinja2==2.11.1
 markdown==3.1.1
 markupsafe==1.1.1         # via jinja2
+pillow==7.0.0
 python-editor==1.0.4      # via inquirer
 python-frontmatter==0.5.0
 pyyaml==5.3               # via python-frontmatter
 python-editor==1.0.4      # via inquirer
 python-frontmatter==0.5.0
 pyyaml==5.3               # via python-frontmatter
index 993c6b6..93ef952 100755 (executable)
@@ -11,4 +11,5 @@ rsync --archive --verbose --compress --delete \
 
 ssh alexwlchan@helene.linode 'chmod 644 ~/sites/books.alexwlchan.net/static/*'
 ssh alexwlchan@helene.linode 'chmod 644 ~/sites/books.alexwlchan.net/covers/*'
 
 ssh alexwlchan@helene.linode 'chmod 644 ~/sites/books.alexwlchan.net/static/*'
 ssh alexwlchan@helene.linode 'chmod 644 ~/sites/books.alexwlchan.net/covers/*'
+ssh alexwlchan@helene.linode 'chmod 644 ~/sites/books.alexwlchan.net/thumbnails/*'
 ssh alexwlchan@helene.linode 'chmod 644 ~/sites/books.alexwlchan.net/**/*.html'
 ssh alexwlchan@helene.linode 'chmod 644 ~/sites/books.alexwlchan.net/**/*.html'
index 573c14d..c55d581 100755 (executable)
@@ -13,6 +13,7 @@ import frontmatter
 from jinja2 import Environment, FileSystemLoader, select_autoescape
 import markdown
 from markdown.extensions.smarty import SmartyExtension
 from jinja2 import Environment, FileSystemLoader, select_autoescape
 import markdown
 from markdown.extensions.smarty import SmartyExtension
+from PIL import Image
 import smartypants
 
 
 import smartypants
 
 
@@ -163,6 +164,27 @@ def render_individual_review(env, *, review_entry):
     out_path.write_text(html)
 
 
     out_path.write_text(html)
 
 
+def _create_new_thumbnail(src_path, dst_path):
+    im = Image.open(src_path)
+    im.thumbnail((240, 240))
+    os.makedirs(os.path.dirname(dst_path), exist_ok=True)
+    im.save(dst_path)
+
+
+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)
+
+        if not os.path.exists(dst_path):
+            _create_new_thumbnail(src_path, dst_path)
+        elif os.stat(src_path).st_mtime > os.stat(dst_path).st_mtime:
+            _create_new_thumbnail(src_path, dst_path)
+
+
 def main():
     env = Environment(
         loader=FileSystemLoader("templates"),
 def main():
     env = Environment(
         loader=FileSystemLoader("templates"),
@@ -173,6 +195,8 @@ def main():
     env.filters["render_date"] = render_date
     env.filters["smartypants"] = smartypants.smartypants
 
     env.filters["render_date"] = render_date
     env.filters["smartypants"] = smartypants.smartypants
 
+    create_thumbnails()
+
     rsync("src/covers/", "_html/covers/")
     rsync("static/", "_html/static/")
 
     rsync("src/covers/", "_html/covers/")
     rsync("static/", "_html/static/")
 
index 2164300..c0a672b 100644 (file)
@@ -1 +1 @@
-<img src="/covers/{{ book.cover_image }}" alt="Cover of {{ book.title }}. {{ book.cover_desc }}">
+<img src="/thumbnails/{{ book.cover_image }}" alt="Cover of {{ book.title }}. {{ book.cover_desc }}">