aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xbin/migrate/2-compress-covers27
1 files changed, 27 insertions, 0 deletions
diff --git a/bin/migrate/2-compress-covers b/bin/migrate/2-compress-covers
new file mode 100755
index 0000000..0398689
--- /dev/null
+++ b/bin/migrate/2-compress-covers
@@ -0,0 +1,27 @@
+#!/usr/bin/env nix-shell
+#!nix-shell -i python3 -p python3Packages.pillow
+
+import PIL.Image
+import glob
+import os
+import sys
+
+if len(sys.argv) == 2 and os.path.exists(sys.argv[1]):
+ book_directory = sys.argv[1]
+else:
+ print(f'Usage: {sys.argv[0]} book-directory')
+ exit(1)
+
+def compress(path):
+ directory = os.path.dirname(os.path.realpath(path))
+ image = PIL.Image.open(path)
+ width, height = image.size
+ if width > 300:
+ image = image.resize((300, int(300 * height / width)), PIL.Image.LANCZOS)
+ image = image.convert('RGB')
+ image.save(f'{directory}/tmp.webp', 'WEBP', optimize=True, quality=85)
+ os.remove(path)
+ os.rename(f'{directory}/tmp.webp', f'{directory}/cover.webp')
+
+for path in glob.glob(f'{book_directory}/**/cover.*', recursive=True):
+ compress(path)