-
Notifications
You must be signed in to change notification settings - Fork 0
/
squares.py
60 lines (46 loc) · 2.36 KB
/
squares.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import os
import sys
from PIL import Image, ImageOps
# Get the path to the source directory from the command line argument
# If no argument is provided, use the current directory as the source
src_dir = os.path.abspath(sys.argv[1]) if len(sys.argv) > 1 else os.getcwd()
# Path to the destination directory where the processed images will be saved
# If the source is a single file, the destination will be the same directory as the source
# If the source is a directory, the destination will be a subdirectory named "processed" inside the source directory
if os.path.isdir(src_dir):
dest_dir = os.path.join(src_dir, 'processed')
else:
dest_dir = os.path.dirname(src_dir)
# Make sure the destination directory exists, create it if it doesn't
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)
# Check if the source is a directory or a single file
if os.path.isdir(src_dir):
# If the source is a directory, loop through all the files in the directory
for filename in os.listdir(src_dir):
# Check if the file is a JPEG image
if filename.endswith(".jpg"):
# Open the image using the Pillow library
image = Image.open(os.path.join(src_dir, filename))
# Get the dimensions of the image
width, height = image.size
# Calculate the size of the shortest side of the image
min_side = min(width, height)
# Create a new image that is a square with the shortest side being 512 pixels
new_image = Image.new('RGB', (512, 512), (255, 255, 255))
# Calculate the starting position of the crop
x_pos = int((width - min_side) / 2)
y_pos = int((height - min_side) / 2)
# Crop the image
cropped_image = image.crop((x_pos, y_pos, x_pos + min_side, y_pos + min_side))
# Resize the cropped image to 512x512 pixels
resized_image = ImageOps.fit(cropped_image, (512, 512), method=Image.Resampling.LANCZOS)
# Paste the resized image onto the new image
new_image.paste(resized_image, (0, 0))
# Save the new image to the destination directory
new_image.save(os.path.join(dest_dir, filename))
else:
# If the source is a single file, open the image using the Pillow library
image = Image.open(src_dir)
# Get the dimensions of the image
width, height