-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresize
executable file
·37 lines (24 loc) · 895 Bytes
/
resize
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
#!/usr/bin/env python3
import sys
import PIL
from PIL import Image
def help_message():
print('Usage: python resizer.py <image> <new-height>')
def main():
if len(sys.argv) == 1 and sys.argv[1] == '-h' or len(sys.argv) < 3:
help_message()
exit(0)
img = sys.argv[1].split('.')
file_name = img[0]
extension = img[1]
image = PIL.Image.open('{}.{}'.format(file_name, extension))
height = int(sys.argv[2])
height_percent = height / image.size[1]
print('Old Dimension: {}'.format(image.size))
print('Image ratio: {}'.format(height_percent))
new_width = int(image.size[0] * height_percent)
print('New dimension: {}, {}'.format(new_width, height))
image = image.resize((new_width, height), PIL.Image.ANTIALIAS)
image.save('{}{}.{}'.format(file_name, str(height), extension))
if __name__ == '__main__':
main()