-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocess_images.py
66 lines (50 loc) · 1.76 KB
/
process_images.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from PIL import Image
import os
from sys import argv
def resize_image(image, width):
return image.resize((width, width))
def square_image(image):
# TODO: Change to crop from the center
smallest_dimension = min(image.size)
return image.crop((0, 0, smallest_dimension, smallest_dimension))
def save_image(image, path):
with open(os.path.abspath(path), 'w+') as file:
image.save(file, "JPEG")
def print_usage():
print('====== Usage ======')
print('python3 resize_images.py <directory> <scale>')
print()
print('directory <str> path of dir with images to process')
print('scale <int> new width of images')
def main(directory, width):
print(directory)
files = os.listdir(directory)
files.sort()
small_directory = os.listdir(os.path.join('data', 'small', 'train'))
print(files)
if '54305.jpg' in small_directory:
print('YES IT IS THERE')
for image_file in files:
if image_file not in small_directory:
#print('file name:', image_file)
with Image.open(os.path.join(directory, image_file)) as image:
# image.show()
squared = square_image(image)
new_image = resize_image(squared, width)
save_path = os.path.join('data', 'small', 'train', image_file)
# new_image.show()
try:
save_image(new_image, save_path)
except:
print('uh oh error, image:', image_file)
print(image_file)
if __name__ == '__main__':
main(os.path.join('data', 'train'), 600)
"""
if len(argv) < 4:
print_usage()
else:
directory = path.join(argv[2])
scale = int(argv[3])
main(directory, scale)
"""