Start using a virtualenv for these scripts
- ID
9973088- date
2023-11-12 00:50:48+00:00- author
Alex Chan <alex@alexwlchan.net>- parent
2a53acb- message
Start using a virtualenv for these scripts- changed files
3 files, 29 additions, 10 deletions
Changed files
images/reborder (933) → images/reborder (1146)
diff --git a/images/reborder b/images/reborder
index c45ece9..eddd8a1 100755
--- a/images/reborder
+++ b/images/reborder
@@ -1,4 +1,4 @@
-#!/usr/bin/env python3
+#!/Users/alexwlchan/repos/scripts/.venv/bin/python3
"""
Adds a white border of consistent width around an image.
@@ -6,6 +6,7 @@ I use it when I've taken a screenshot of something on a white background,
and I want to tidy up the crop quickly.
"""
+import argparse
import itertools
import os
import sys
@@ -13,14 +14,20 @@ import sys
from PIL import Image, ImageChops, ImageOps
+def parse_args():
+ parser = argparse.ArgumentParser()
+
+ parser.add_argument("PATH")
+ parser.add_argument("BORDER_WIDTH", type=int)
+ parser.add_argument("--in-place", action="store_true")
+
+ return parser.parse_args()
+
+
if __name__ == '__main__':
- try:
- path = sys.argv[1]
- border_width = int(sys.argv[2])
- except (IndexError, ValueError):
- sys.exit(f"Usage: {__file__} <PATH> <BORDER_WIDTH>")
+ args = parse_args()
- im = Image.open(path)
+ im = Image.open(args.PATH)
bg = Image.new(im.mode, im.size, (255, 255, 255))
@@ -30,11 +37,14 @@ if __name__ == '__main__':
if bbox:
im = im.crop(bbox)
- im = ImageOps.expand(im, border=border_width, fill='white')
+ im = ImageOps.expand(im, border=args.BORDER_WIDTH, fill='white')
- name, ext = os.path.splitext(path)
+ name, ext = os.path.splitext(args.PATH)
- out_path = f'{name}.reborder_{border_width}{ext}'
+ if args.in_place:
+ out_path = args.PATH
+ else:
+ out_path = f'{name}.reborder_{args.BORDER_WIDTH}{ext}'
im.save(out_path)
requirements.in (0) → requirements.in (7)
diff --git a/requirements.in b/requirements.in
new file mode 100644
index 0000000..7e2fba5
--- /dev/null
+++ b/requirements.in
@@ -0,0 +1 @@
+Pillow
requirements.txt (0) → requirements.txt (172)
diff --git a/requirements.txt b/requirements.txt
new file mode 100644
index 0000000..5284de9
--- /dev/null
+++ b/requirements.txt
@@ -0,0 +1,8 @@
+#
+# This file is autogenerated by pip-compile with Python 3.12
+# by the following command:
+#
+# pip-compile requirements.in
+#
+pillow==10.1.0
+ # via -r requirements.in