Skip to content

Commit 3d5d32b

Browse files
committed
Add a quick script to get a square crop of images
1 parent 873b071 commit 3d5d32b

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

config.fish

+1
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ __create_python_script_alias images/images_only_pdf.py
199199
__create_python_script_alias images/pdfthumb.py
200200
__create_python_script_alias images/reborder.py
201201
__create_python_script_alias images/save_xkcd.py
202+
__create_python_script_alias images/squarify.py
202203
__create_python_script_alias images/srgbify.py
203204
__create_python_script_alias images/tint_image.py
204205
__create_python_script_alias text/fix_twemoji.py

images/README.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ scripts = [
131131
saves a single comic from <a href="https://xkcd.com/">xkcd</a>, plus some metadata.
132132
"""
133133
},
134+
{
135+
"usage": "squarify.py [PATH]",
136+
"description": """
137+
crop an image to a central square
138+
"""
139+
},
134140
{
135141
"usage": "srgbify.py [PATH]",
136142
"description": """
@@ -295,6 +301,15 @@ cog_helpers.create_description_table(folder_name=folder_name, scripts=scripts)
295301
saves a single comic from <a href="https://xkcd.com/">xkcd</a>, plus some metadata.
296302
</dd>
297303

304+
<dt>
305+
<a href="https://github.com/alexwlchan/scripts/blob/main/images/squarify.py">
306+
<code>squarify.py [PATH]</code>
307+
</a>
308+
</dt>
309+
<dd>
310+
crop an image to a central square
311+
</dd>
312+
298313
<dt>
299314
<a href="https://github.com/alexwlchan/scripts/blob/main/images/srgbify.py">
300315
<code>srgbify.py [PATH]</code>
@@ -324,4 +339,4 @@ cog_helpers.create_description_table(folder_name=folder_name, scripts=scripts)
324339
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.
325340
</dd>
326341
</dl>
327-
<!-- [[[end]]] (checksum: c65a5837a2a9b2e8cab77b6b040d081f) -->
342+
<!-- [[[end]]] (checksum: 72dd4bb73da2c34197f1d6816eacb980) -->

images/squarify.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Creates a square crop of an image.
4+
"""
5+
6+
import pathlib
7+
import sys
8+
9+
from PIL import Image
10+
11+
12+
if __name__ == '__main__':
13+
try:
14+
path = pathlib.Path(sys.argv[1])
15+
except IndexError:
16+
sys.exit(f"Usage: {__file__} IMAGE_PATH")
17+
18+
im = Image.open(path)
19+
20+
out_path = path.with_suffix(".square" + path.suffix)
21+
assert out_path != path
22+
23+
if im.width == im.height:
24+
print(path)
25+
elif im.width > im.height:
26+
left = (im.width - im.height) / 2
27+
upper = 0
28+
right = im.width - (im.width - im.height) / 2
29+
lower = im.height
30+
31+
crop_im = im.crop((left, upper, right, lower))
32+
crop_im.save(out_path)
33+
print(out_path)
34+
elif im.height > im.width:
35+
left = 0
36+
upper = (im.height - im.width) / 2
37+
right = im.width
38+
lower = im.height - (im.height - im.width) / 2
39+
40+
crop_im = im.crop((left, upper, right, lower))
41+
crop_im.save(out_path)
42+
print(out_path)
43+
44+
45+
# main()

0 commit comments

Comments
 (0)