-
Notifications
You must be signed in to change notification settings - Fork 75
/
crop_thesis_covers.py
28 lines (25 loc) · 1.2 KB
/
crop_thesis_covers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import pathlib
import imageio.v3 as iio
target_aspect_ratio = 0.71
imgdir = pathlib.Path("./content/images/theses")
for extension in ['.png', '.jpeg', '.jpg']:
for imgfile in imgdir.glob(f"*{extension}"):
# Read image and compute current aspect ratio
image = iio.imread(imgfile)
aspect_ratio = image.shape[1] / image.shape[0]
shape = image.shape
# Crop image to match target aspect ratio
if aspect_ratio > target_aspect_ratio:
new_width = round(target_aspect_ratio * image.shape[0])
if image.shape[1] - new_width > 1: # ignore 1 pixel differences
offset = (image.shape[1] - new_width) // 2
image = image[:, offset : offset + new_width]
else:
new_height = round(image.shape[1] / target_aspect_ratio)
if image.shape[0] - new_height > 1:
offset = (image.shape[0] - new_height) // 2
image = image[offset : offset + new_height, :]
# If the image size has changed, overwrite image file
if image.shape != shape:
print(f"Resized {imgfile.relative_to(imgdir)} from {shape} to {image.shape}")
iio.imwrite(imgfile, image)