forked from Py-Contributors/awesomeScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_resizer.py
50 lines (37 loc) · 1.32 KB
/
image_resizer.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
import sys
import os.path
from argparse import ArgumentParser
from PIL import Image
parser = ArgumentParser()
parser.add_argument('--file', dest='File', help='Input image filename',
required=True)
parser.add_argument('--width', dest='Width', help='Width in pixels',
type=int)
parser.add_argument('--height', dest='Height', help='Height in pixels',
type=int)
args = parser.parse_args()
if not args.Width and not args.Height:
print('Error: Either width or height must be specified')
sys.exit(1)
def get_output_dimensions(input_image, args):
width, height = input_image.size
if not args.Width:
ratio = args.Height / height
output_width = round(width * ratio)
else:
output_width = args.Width
if not args.Height:
ratio = args.Width / width
output_height = round(height * ratio)
else:
output_height = args.Height
return (output_width, output_height)
try:
input_image = Image.open(args.File)
width, height = get_output_dimensions(input_image, args)
resized_image = input_image.resize((width, height))
file_extension = os.path.splitext(args.File)[1]
resized_image.save('output%s' % (file_extension))
except Exception as err:
print('Could not resize image: %s' % (err))
sys.exit(1)