-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBatchResizer.py
More file actions
73 lines (66 loc) · 2.61 KB
/
BatchResizer.py
File metadata and controls
73 lines (66 loc) · 2.61 KB
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
67
68
69
70
71
72
73
# To run this script, PIL or Pillow should be installed
# Also jpeg and png decoders (libraries) should be installed with PIl or Pillow
# Part of this code is from: http://pythoncentral.io/resize-image-python-batch/
import os
# Next is a parser for GETting command line OPTions
import getopt
import sys
from PIL import Image
# Parse the arguments
opts, args = getopt.getopt(sys.argv[1:], 'd:w:h:')
# Set 'empty' default values to the needed variables, so we can check passed arguments
directory = ''
width = -1
height = -1
# If an argument was passed in, assign it to the correct variable
for opt, arg in opts:
if opt == '-d':
directory = arg
elif opt == '-w':
width = int(arg)
elif opt == '-h':
height = int(arg)
# Check passed arguments
if width == -1 or height == -1 or directory == '':
print('Invalid command line arguments. -d [directory] ' \
'-w [width in px] -h [height in px] are required')
# example: python BatchResizer.py -d /Users/Name/Desktop/Photos -w 405 -h 405
# If an argument is missing exit the application.
exit()
# Iterate through every image given in the directory argument and resize it
for image in os.listdir(directory):
# Next line is to solve a hidden file bug
if image == ".DS_Store":
pass
else:
print('Resizing image ' + image)
# Open the file
img = Image.open(os.path.join(directory, image))
# Check whether file is landscape or portrait
original_width, original_height = img.size
if original_width > original_height:
# For landscape images
baseWidth = width
# Calculate the height using the same aspect ratio
widthPercent = (baseWidth / float(img.size[0]))
ratioHeight = int((float(img.size[1]) * float(widthPercent)))
# Resize it.
img = img.resize((baseWidth, ratioHeight), Image.BILINEAR)
# Save it back to disk.
img.save(os.path.join(directory, 'thumb-' + image))
elif original_width < original_height:
# For portrait images
baseHeight = height
# Calculate the height using the same aspect ratio
heightPercent = (baseHeight / float(img.size[1]))
ratioWidth = int((float(img.size[0]) * float(heightPercent)))
# Resize it.
img = img.resize((ratioWidth, baseHeight), Image.BILINEAR)
# Save it back to disk.
img.save(os.path.join(directory, 'thumb-' + image))
else: # image must be square already then
# Resize it.
img = img.resize((width, height), Image.BILINEAR)
# Save it back to disk.
img.save(os.path.join(directory, 'thumb-' + image))
print('Batch processing complete.')