-
Notifications
You must be signed in to change notification settings - Fork 1
/
resize.py
74 lines (50 loc) · 1.82 KB
/
resize.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
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Date : 2015-08-04 22:06:17
# @Author : Alexa ([email protected])
# @Link :
# @Disc :
from PIL import Image
import sys
import os.path
def resize_img(name, output_name):
print("resize: %s -> output_name%s"%(name,output_name))
output_name_tail = output_name[ output_name.rfind(".") : ]
img_3x_name = output_name.replace(output_name_tail,"@3x" + output_name_tail)
img_2x_name = output_name.replace(output_name_tail,"@2x" + output_name_tail)
img_1x_name = output_name
if os.path.exists( img_3x_name ) and os.path.exists( img_2x_name ) and os.path.exists( img_1x_name ):
print("resized image already existed")
return
img_big = Image.open( name )
if "@3x" in name:
multiple = 1
elif "@6x" in name:
multiple = 2
else:
raise Exception("Must input a @6x or @3x file name")
img_3x = img_big.resize(( int(img_big.size[0]/(multiple)),int(img_big.size[1]/(multiple)) ), Image.BICUBIC)
img_2x = img_big.resize(( int(img_big.size[0]/(1.5*multiple)),int(img_big.size[1]/(1.5*multiple)) ), Image.BICUBIC)
img_1x = img_big.resize(( int(img_big.size[0]/(3*multiple)),int(img_big.size[1]/(3*multiple)) ), Image.BICUBIC)
img_3x.save( img_3x_name )
img_2x.save( img_2x_name )
img_1x.save( img_1x_name )
def show_help():
help_doc = '''commands:
'''
print(help_doc)
def main():
if len(sys.argv) == 1:
show_help()
return
cmd = sys.argv[1]
if cmd != "resize":
show_help()
items = sys.argv[2:]
for name in items:
resize_img( name, name.replace("@6x","").replace("@3x",""))
if __name__ == '__main__':
main()
else:
print ('resize.py had been imported as a module')