-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresize_images.py
More file actions
46 lines (30 loc) · 1.16 KB
/
resize_images.py
File metadata and controls
46 lines (30 loc) · 1.16 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
from PIL import Image
import os, glob
import argparse
parser = argparse.ArgumentParser(
prog="Bulk Image Resizer",
description="Takes all JPG and PNG files in current directory & resizes width depending on command line arg"
)
parser.add_argument("max_width", type=int, help="If an image is larger than this the width is resized to equal this width")
argz = parser.parse_args()
print(argz.max_width)
breakpoint()
def resize_to_width(path_in, path_out, new_width):
img = Image.open(path_in)
w, h = img.size
# calculate new height preserving aspect ratio
new_height = int(h * (new_width / w))
# resize
resized = img.resize((new_width, new_height))
resized.save(path_out)
image_files = glob.glob("*.[jJ][pP][gG]") + glob.glob("*.[pP][nN][gG]")
count = 0
for image_file in image_files:
with Image.open(image_file) as image:
file, ext = os.path.splitext(image_file)
w, h = image.size
if w > 2499:
print(f"resizing: {image_file}")
resize_to_width(image_file, f"resized/{file}__resized{ext.lower()}", 1500)
count += 1
print(f"resized {count} images")