Skip to main content

Use the virtualenv for kn_cover_image

ID
5c04ed1
date
2023-11-19 23:26:05+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
9c9bbfb
message
Use the virtualenv for kn_cover_image
changed files
5 files, 23 additions, 14 deletions

Changed files

.with-venv-python/kn_cover_image (0) → .with-venv-python/kn_cover_image (132)

diff --git a/.with-venv-python/kn_cover_image b/.with-venv-python/kn_cover_image
new file mode 100755
index 0000000..f631654
--- /dev/null
+++ b/.with-venv-python/kn_cover_image
@@ -0,0 +1,6 @@
+#!/usr/bin/env bash
+
+set -o errexit
+set -o nounset
+
+~/repos/scripts/.venv/bin/python3 ~/repos/scripts/images/kn_cover_image.py "$@"

images/README.md (5792) → images/README.md (5798)

diff --git a/images/README.md b/images/README.md
index c921081..006ef63 100644
--- a/images/README.md
+++ b/images/README.md
@@ -83,8 +83,8 @@ These scripts are for working with images and other visual material.
   </dd>
 
   <dt>
-    <a href="https://github.com/alexwlchan/scripts/blob/main/images/kn_cover_image">
-      <code>kn_cover_image [PATH]</code>
+    <a href="https://github.com/alexwlchan/scripts/blob/main/images/kn_cover_image.py">
+      <code>kn_cover_image.py [PATH]</code>
     </a>
   </dt>
   <dd>

images/kn_cover_image (5274) → images/kn_cover_image.py (5274)

diff --git a/images/kn_cover_image b/images/kn_cover_image.py
similarity index 90%
rename from images/kn_cover_image
rename to images/kn_cover_image.py
index 937a4e4..be999d0 100755
--- a/images/kn_cover_image
+++ b/images/kn_cover_image.py
@@ -8,14 +8,12 @@ as images, so you can see how the script is working.
 """
 
 import collections
-import io
-import itertools
 import math
 import os
 import subprocess
 import sys
 
-from PIL import Image, ImageCms
+from PIL import Image
 import termcolor
 
 
@@ -69,7 +67,7 @@ if __name__ == "__main__":
     #
     corner_pixels = set()
 
-    for (x, y) in white_pixels:
+    for x, y in white_pixels:
         diagonal = {(x - 1, y - 1), (x - 1, y + 1), (x + 1, y - 1), (x + 1, y + 1)}
         orthogonal = {(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)}
 
@@ -93,7 +91,7 @@ if __name__ == "__main__":
     #
     column_corners = collections.defaultdict(list)
 
-    for (x, y) in corner_pixels:
+    for x, y in corner_pixels:
         column_corners[x].append(y)
 
     # Now look for columns with:
@@ -107,7 +105,7 @@ if __name__ == "__main__":
     #
     rectangles = collections.defaultdict(list)
 
-    for (x, y_coords) in column_corners.items():
+    for x, y_coords in column_corners.items():
         if len(y_coords) == 2:
             rectangles[tuple(sorted(y_coords))].append(x)
 
@@ -156,6 +154,13 @@ if __name__ == "__main__":
         y0 += int(math.ceil(diff / 2))
         y1 -= int(math.floor(diff / 2))
 
+    # Account for the case where odd/even offsets mean we're actually
+    # one away, e.g. 1601 × 800
+    if x1 - x0 == (y1 - y0) * 2 + 1:
+        x0 += 1
+
+    assert (x1 - x0) == (y1 - y0) * 2
+
     # Now save the image to an appropriate filename, and tell the user
     cropped_im = im.crop((x0, y0, x1, y1))
 
@@ -167,12 +172,7 @@ if __name__ == "__main__":
 
     if im.info.get("icc_profile") is not None:
         subprocess.check_call(
-            [
-                "retrobatch",
-                "--workflow",
-                "~/repos/scripts/images/overwrite_with_srgb.retrobatch",
-                out_path,
-            ],
+            ["srgbify", out_path],
             stdout=subprocess.DEVNULL,
         )
 

requirements.in (48) → requirements.in (58)

diff --git a/requirements.in b/requirements.in
index d7a101c..0040891 100644
--- a/requirements.in
+++ b/requirements.in
@@ -4,3 +4,4 @@ hyperlink
 keyring
 Pillow
 pip-tools
+termcolor

requirements.txt (1020) → requirements.txt (1066)

diff --git a/requirements.txt b/requirements.txt
index 8387d65..2813c45 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -46,6 +46,8 @@ pyflakes==3.1.0
     # via flake8
 pyproject-hooks==1.0.0
     # via build
+termcolor==2.3.0
+    # via -r requirements.in
 wheel==0.41.3
     # via pip-tools