aboutsummaryrefslogtreecommitdiff
path: root/convert_photo.py
diff options
context:
space:
mode:
Diffstat (limited to 'convert_photo.py')
-rw-r--r--convert_photo.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/convert_photo.py b/convert_photo.py
new file mode 100644
index 0000000..b1c9063
--- /dev/null
+++ b/convert_photo.py
@@ -0,0 +1,29 @@
+import argparse
+
+from PIL import Image, ImageOps
+
+from epdify import epdify, get_crop_box_and_orientation
+
+parser = argparse.ArgumentParser()
+parser.add_argument("--dither-palette", default="perceived")
+parser.add_argument("--final-palette", default="native")
+parser.add_argument("-o", "--output-filename")
+parser.add_argument("photo_filename")
+args = parser.parse_args()
+
+img = Image.open(args.photo_filename)
+img = ImageOps.exif_transpose(img)
+is_portrait = img.width < img.height
+box, orientation = get_crop_box_and_orientation(img.width, img.height, 0.6)
+if orientation == "landscape":
+ size = (800, 480)
+else:
+ size = (480, 800)
+img = img.resize(size, resample=Image.Resampling.LANCZOS, box=box, reducing_gap=3)
+img = epdify(img, args.dither_palette, args.final_palette)
+if args.output_filename is None:
+ base, _ = args.photo_filename.rsplit('.', 1)
+ out_filename = f"{base}.epdified.bmp"
+else:
+ out_filename = args.output_filename
+img.save(out_filename, 'bmp')