-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathimage_resizer.py
More file actions
55 lines (44 loc) · 1.49 KB
/
image_resizer.py
File metadata and controls
55 lines (44 loc) · 1.49 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
#This file change the size of images
#http://pythoncentral.io/resize-image-python-batch/
#Usage: python image_resizer.py -d 'PATHHERE' -w 448 -h 200
import os
import getopt
import sys
from PIL import Image
#Parsing the image
opts, args = getopt.getopt(sys.argv[1:], 'd:w:h')
# Set some default values to the needed variables.
directory = ''
width = -1
height = -1
# Let's parse the arguments.
opts, args = getopt.getopt(sys.argv[1:], 'd:w:h:')
# Set some default values to the needed variables.
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)
# We have to make sure that all of the arguments were passed.
if width == -1 or height == -1 or directory == '':
print('Invalid command line arguments. -d [directory] ' \
'-w [width] -h [height] are required')
# 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):
print('Resizing image ' + image)
# Open the image file
img = Image.open(os.path.join(directory, image))
# Resize it
#img = img.resize((width, height), Image.BILINEAR)
img = img.resize((width, height), Image.ANTIALIAS)
# Save it back to disk.
img.save(os.path.join(directory, '' + image))
print('Batch processing complete.')