-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpad.py
46 lines (39 loc) · 1.7 KB
/
pad.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
import os
from PIL import Image
def pad_image_to_16_9(image_path):
with Image.open(image_path) as im:
# Calculate the desired size while maintaining the aspect ratio
width, height = im.size
aspect_ratio = width / height
target_aspect_ratio = 16 / 9
# Check which dimension (width or height) should be adjusted
if aspect_ratio < target_aspect_ratio:
# Image is too tall, need to add padding to the width
new_width = int(target_aspect_ratio * height)
result = Image.new("RGB", (new_width, height), color=(0, 0, 0))
result.paste(im, ((new_width - width) // 2, 0))
elif aspect_ratio > target_aspect_ratio:
# Image is too wide, need to add padding to the height
new_height = int(width / target_aspect_ratio)
result = Image.new("RGB", (width, new_height), color=(0, 0, 0))
result.paste(im, (0, (new_height - height) // 2))
else:
# Image already has a 16:9 aspect ratio
return im
return result
def save_padded_image(image, original_path):
# Save the padded image
new_filename = f"padded_{os.path.basename(original_path)}"
image.save(new_filename)
print(f"Saved: {new_filename}")
def main():
files_in_current_directory = os.listdir('.')
for file_name in files_in_current_directory:
if file_name.lower().endswith('.png'):
try:
padded_image = pad_image_to_16_9(file_name)
save_padded_image(padded_image, file_name)
except Exception as e:
print(f"Error processing {file_name}: {e}")
if __name__ == "__main__":
main()