-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscaleImage.py
58 lines (50 loc) · 2.33 KB
/
scaleImage.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
from PIL import Image
import PIL
import sys
import os
from os.path import isfile
"""
This script resizes the source image to percents of the original image size. Rescaled images are dumped in the same directory as the source image and a list of the paths to the new images is returned for easier iteration later.
To run call main(path, high, low, inc) where:
path is the path to the input image
high is the highest percentage version of the image to create
low is the lowest percentage version of the image to create
inc is the value to increment between high and low
Notes:
Technically you could provide a HIGH: value above 100
To run once at X resolution size set HIGH:X LOW:X INC:1. inc cannot be 0.
The incrementing starts from high so the exact low value might not be run if the increment value doesn't coincide with the difference between the two. (high-low)%inc != 0 (i.e. HIGH:100 LOW:90 INC:6 would run on 100% and 94%)
"""
def main(path, high, low, inc):
#Check that input path is valid
if not isinstance(path, str):
print('ERROR in scaleImage: Path must be string.')
return []
if not os.path.exists(path):
print('ERROR in scaleImage: Path does not exist.')
return []
if not isfile(path):
print('ERROR in scaleImage: Path does not point to a file.')
return []
filename, file_ext = os.path.splitext(path)
#Make sure the file is an image
valid_filetypes = ('png', 'tif', 'tiff', 'jpg', 'jpeg', 'bmp', 'webp')
if not file_ext[1:] in valid_filetypes:
print(f'ERROR in scaleImage: {file_ext} is an invalid file type. {valid_filetypes} are all accepted types.')
return []
#Stores the percent sizes to rescale to
i = high
percent_list = []
# Populate percent_list. Essentially just range() but backwards
while i >= low:
percent_list.append(i)
i = i - inc
#Scale image
scaled_image_list = []
for n in percent_list:
#Resize image and time
image = Image.open(path)
image.resize((round(image.size[0]*(n/100)), round(image.size[1]*(n/100)))).save(f'{filename}_{n}{file_ext}')
image.close()
scaled_image_list.append(f'{filename}_{n}{file_ext}')
return scaled_image_list