-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
49 lines (34 loc) · 1.24 KB
/
main.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
import os
import sys
from PIL import Image
def resize(file_path: str, output_directory: str) -> bool:
'''
Resize the image to 128x128 and save it to the same folder
:param file_path: path to the image
:param output_directory: path to the output directory
:return: True if the image was resized, False otherwise
'''
size = 128, 128
file, ext = os.path.splitext(file_path)
file_name = os.path.basename(file)
if ext != '.jpg':
return False
with Image.open(file_path) as img:
img.thumbnail(size)
img.save(output_directory + '/' + file_name + "-resized.jpg", 'JPEG')
return True
# MAIN PROGRAM
path = sys.argv[1]
if os.path.isfile(path):
resize(path, os.path.dirname(path))
print("Resized " + os.path.basename(path) + " and saved it to " + os.path.dirname(path))
elif os.path.isdir(path):
output_directory = os.path.dirname(path) + "/resized_images"
if not os.path.exists(output_directory):
os.makedirs(output_directory)
count = 0
for file in os.listdir(path):
result = resize(os.path.join(path, file), output_directory)
if result:
count += 1
print("Resized " + str(count) + " images and saved them to " + output_directory)