Skip to main content

add some debug info

ID
2f720b3
date
2023-05-29 21:17:37+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
425d245
message
add some debug info
changed files
1 file, 34 additions

Changed files

images/kn_cover_image (4121) → images/kn_cover_image (5191)

diff --git a/images/kn_cover_image b/images/kn_cover_image
index f07976e..31d376e 100755
--- a/images/kn_cover_image
+++ b/images/kn_cover_image
@@ -1,6 +1,10 @@
 #!/usr/bin/env python3
 """
 Prepare a cover image for my website, based on an image I've created in Keynote.
+
+You can add the --debug flag to get the intermediate stages saved
+as images, so you can see how the script is working.
+
 """
 
 import collections
@@ -30,6 +34,9 @@ if __name__ == "__main__":
     if im.mode != "RGB":
         sys.exit(f"Unsupported image mode: {im.mode}")
 
+    name, ext = os.path.splitext(path)
+    ext = ext.replace(".jpeg", ".jpg")
+
     # Find all the positions with white pixels in the image.
     #
     # This assumes that Keynote may not always get perfectly white, so
@@ -41,6 +48,14 @@ if __name__ == "__main__":
         if min(im.getpixel((x, y))) >= 250
     }
 
+    if "--debug" in sys.argv:
+        im_white = Image.new("RGB", size=im.size)
+
+        for p in white_pixels:
+            im_white.putpixel(p, (255, 0, 0))
+
+        im_white.save(f"{name}.debug-1-white_pixels{ext}")
+
     # Now find all the white "inner corner" pixels -- that is, pixels
     # surrounded on three corners by white pixels, but not the fourth.
     #
@@ -64,6 +79,14 @@ if __name__ == "__main__":
         ):
             corner_pixels.add((x, y))
 
+    if "--debug" in sys.argv:
+        im_corner = Image.new("RGB", size=im.size)
+
+        for p in corner_pixels:
+            im_corner.putpixel(p, (255, 0, 0))
+
+        im_corner.save(f"{name}.debug-2-corner_pixels{ext}")
+
     # Group the corners by row.
     #
     # The `corners_by_col` is [x: {y-coords of corners with this y-coord}]
@@ -94,6 +117,17 @@ if __name__ == "__main__":
         if len(x_coords) == 2
     }
 
+    if "--debug" in sys.argv:
+        im_corner = Image.new("RGB", size=im.size, color=(255, 255, 255))
+
+        for (y0, y1), (x0, x1) in rectangles.items():
+            im_corner.putpixel((x0, y0), (0, 0, 0))
+            im_corner.putpixel((x0, y1), (0, 0, 0))
+            im_corner.putpixel((x1, y0), (0, 0, 0))
+            im_corner.putpixel((x1, y1), (0, 0, 0))
+
+        im_corner.save(f"{name}.debug-3-rectangles{ext}")
+
     # The outline rectangle is the biggest rectangle; sort by area.
     (y0, y1), (x0, x1) = max(
         rectangles.items(), key=lambda xy: (xy[1][0] - xy[1][1]) * (xy[0][0] - xy[0][1])