-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResizeImage.py
59 lines (47 loc) · 1.59 KB
/
ResizeImage.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
from PIL import Image
from resizeimage import resizeimage
import os
def initial_image_folder():
'''
Set the initial photo for your image
:return: String Folder Location
'''
return 'GivenImages'
def output_image_folder():
'''
Set the output for your updated photos
:return: String Folder Location
'''
return 'OutputImages'
def create_input_folder():
'''
Creates folder for input images
'''
os.mkdir(initial_image_folder())
def create_output_folder():
'''
Creates folder for output
'''
os.mkdir(output_image_folder())
def image_dimensions(height, width):
return [round(height*.15),round(width*.15)]
def main():
#Test if folders exist
if not os.path.exists(output_image_folder()):
create_output_folder()
if not os.path.exists(initial_image_folder()):
create_input_folder()
#iterate through initial image folder
for file in os.listdir(initial_image_folder()):
#Creates file names
file_name_input = initial_image_folder()+'\\' + file
file_name_output = output_image_folder()+'\\' + file
#Checks file extension
if file.endswith(".jpg") or file.endswith(".png"):
with open(file_name_input,'r+b') as f:
with Image.open(f) as image:
#Adjustes image and saves the image
updated_image = resizeimage.resize_thumbnail(image, image_dimensions(image.height, image.width), Image.ANTIALIAS)
updated_image.save(file_name_output, image.format)
print("Images have been resized");
main()