-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpaddington.py
52 lines (40 loc) · 1.76 KB
/
paddington.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
import os
from PIL import Image, ImageOps
from tqdm import tqdm
# Editable variables
source_folder = "source-images"
output_folder = "output-images"
padding_ratio = 100 # Set padding as percentage, set 0 for hardcrop
image_ratio = None # Optionally set image ratio: (width, height) or None
def calculate_padding(img_size, padding_ratio):
return int(img_size * (padding_ratio / 100))
def expand_canvas_to_ratio(img, ratio):
img_width, img_height = img.size
target_width = max(img_width, int(img_height * ratio[0] / ratio[1]))
target_height = max(img_height, int(img_width * ratio[1] / ratio[0]))
new_img = Image.new("RGBA", (target_width, target_height), (0, 0, 0, 0))
new_img.paste(img, ((target_width - img_width) // 2, (target_height - img_height) // 2))
return new_img
def add_padding(img, padding):
border = (padding, padding, padding, padding)
return ImageOps.expand(img, border, fill=(0, 0, 0, 0))
if not os.path.exists(output_folder):
os.makedirs(output_folder)
image_files = [
f for f in os.listdir(source_folder) if f.lower().endswith((".png", ".webp"))
]
for file in tqdm(image_files, desc="Padding & resizing images", unit="image"):
image_path = os.path.join(source_folder, file)
img = Image.open(image_path).convert("RGBA")
# Hardcrop: remove transparency around the image
img = img.crop(img.getbbox())
# Expand canvas to the required ratio (if provided)
if image_ratio is not None:
img = expand_canvas_to_ratio(img, image_ratio)
# Apply padding
if padding_ratio > 0:
padding = calculate_padding(img.size[0], padding_ratio)
img = add_padding(img, padding)
# Save resized image
output_path = os.path.join(output_folder, file)
img.save(output_path, quality=90)