Skip to main content

Add a quick script to get a square crop of images

ID
3d5d32b
date
2025-01-12 21:51:50+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
873b071
message
Add a quick script to get a square crop of images
changed files
3 files, 62 additions, 1 deletion

Changed files

config.fish (6917) → config.fish (6965)

diff --git a/config.fish b/config.fish
index f473d35..1a4dd56 100644
--- a/config.fish
+++ b/config.fish
@@ -199,6 +199,7 @@ __create_python_script_alias images/images_only_pdf.py
 __create_python_script_alias images/pdfthumb.py
 __create_python_script_alias images/reborder.py
 __create_python_script_alias images/save_xkcd.py
+__create_python_script_alias images/squarify.py
 __create_python_script_alias images/srgbify.py
 __create_python_script_alias images/tint_image.py
 __create_python_script_alias text/fix_twemoji.py

images/README.md (10652) → images/README.md (10983)

diff --git a/images/README.md b/images/README.md
index 9c10da2..876a9eb 100644
--- a/images/README.md
+++ b/images/README.md
@@ -132,6 +132,12 @@ scripts = [
         """
     },
     {
+        "usage": "squarify.py [PATH]",
+        "description": """
+        crop an image to a central square
+        """
+    },
+    {
         "usage": "srgbify.py [PATH]",
         "description": """
         convert an image in-place to have an sRGB colour profile.
@@ -296,6 +302,15 @@ cog_helpers.create_description_table(folder_name=folder_name, scripts=scripts)
   </dd>
 
   <dt>
+    <a href="https://github.com/alexwlchan/scripts/blob/main/images/squarify.py">
+      <code>squarify.py [PATH]</code>
+    </a>
+  </dt>
+  <dd>
+    crop an image to a central square
+  </dd>
+
+  <dt>
     <a href="https://github.com/alexwlchan/scripts/blob/main/images/srgbify.py">
       <code>srgbify.py [PATH]</code>
     </a>
@@ -324,4 +339,4 @@ cog_helpers.create_description_table(folder_name=folder_name, scripts=scripts)
     I don’t use this script very often, but I checked it in because I thought it was a neat trick I didn’t want to forget.
   </dd>
 </dl>
-<!-- [[[end]]] (checksum: c65a5837a2a9b2e8cab77b6b040d081f) -->
+<!-- [[[end]]] (checksum: 72dd4bb73da2c34197f1d6816eacb980) -->

images/squarify.py (0) → images/squarify.py (1046)

diff --git a/images/squarify.py b/images/squarify.py
new file mode 100755
index 0000000..5472b49
--- /dev/null
+++ b/images/squarify.py
@@ -0,0 +1,45 @@
+#!/usr/bin/env python3
+"""
+Creates a square crop of an image.
+"""
+
+import pathlib
+import sys
+
+from PIL import Image
+
+
+if __name__ == '__main__':
+    try:
+        path = pathlib.Path(sys.argv[1])
+    except IndexError:
+        sys.exit(f"Usage: {__file__} IMAGE_PATH")
+    
+    im = Image.open(path)
+    
+    out_path = path.with_suffix(".square" + path.suffix)
+    assert out_path != path
+    
+    if im.width == im.height:
+        print(path)
+    elif im.width > im.height:
+        left = (im.width - im.height) / 2
+        upper = 0
+        right = im.width - (im.width - im.height) / 2
+        lower = im.height
+        
+        crop_im = im.crop((left, upper, right, lower))
+        crop_im.save(out_path)
+        print(out_path)
+    elif im.height > im.width:
+        left = 0
+        upper = (im.height - im.width) / 2
+        right = im.width
+        lower = im.height - (im.height - im.width) / 2
+        
+        crop_im = im.crop((left, upper, right, lower))
+        crop_im.save(out_path)
+        print(out_path)
+    
+    
+    # main()
\ No newline at end of file