-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresize-n-save.py
38 lines (30 loc) · 1.29 KB
/
resize-n-save.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
import os
import argparse
import PIL.Image as PILI
# Comply with os.makedirs - should not use parent dir. (..) in out_dir
ap = argparse.ArgumentParser()
ap.add_argument("in_dir") # e.g. "inat18/"
ap.add_argument("out_dir") # e.g. "out/"
ap.add_argument("--new_size", default=128)
args = ap.parse_args()
in_dir = args.in_dir
out_dir = args.out_dir
new_size = int(args.new_size)
for dirpath, dirnames, filenames in os.walk(in_dir):
for img_name in filenames:
# Skips empty directories
img_path = dirpath + "/" + img_name
resized_img = PILI.open(img_path).resize((new_size, new_size), PILI.ANTIALIAS).convert("RGB")
# Ensure jpg output
# TODO: Does not consider formats other than png. Needs better filename detection
if img_name.endswith(".png"):
resized_img_name = img_name[:-3] + "jpg"
elif img_name.endswith(".jpeg"):
resized_img_name = img_name[:-4] + "jpg"
else:
resized_img_name = img_name
save_dir = out_dir + dirpath[len(in_dir):]
os.makedirs(save_dir, exist_ok=True)
save_path = save_dir + "/" + resized_img_name
resized_img.save(save_path, subsampling=0, quality=100) # Lose as little quality as possible
print("Saved image:", save_path)