-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_batch_resizer.py
More file actions
47 lines (33 loc) · 1.45 KB
/
Copy pathimage_batch_resizer.py
File metadata and controls
47 lines (33 loc) · 1.45 KB
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
from PIL import Image # Pillow library for image processing
import os # work with files and folders
# Folder containing your images
input_folder = os.path.expanduser("~/Downloads") # change if needed
# Folder to save resized images (keeps originals safe)
output_folder = os.path.join(input_folder, "resized")
os.makedirs(output_folder, exist_ok=True)
# Desired max width (height will adjust automatically)
max_width = 800
# Go through all files in the folder
for filename in os.listdir(input_folder):
input_path = os.path.join(input_folder, filename)
# Skip folders
if os.path.isdir(input_path):
continue
# Only process image files (basic check)
if not filename.lower().endswith((".jpg", ".jpeg", ".png", ".webp")):
continue
try:
# Open the image
with Image.open(input_path) as img:
# Get current size
original_width, original_height = img.size
# Calculate new height to keep aspect ratio
new_height = int((max_width / original_width) * original_height)
# Resize the image
resized_img = img.resize((max_width, new_height))
# Save resized image to output folder
output_path = os.path.join(output_folder, filename)
resized_img.save(output_path)
print(f"Resized: {filename} → {max_width}x{new_height}")
except Exception as e:
print(f"Error processing {filename}: {e}")