-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhw18.py
More file actions
80 lines (64 loc) · 2.2 KB
/
hw18.py
File metadata and controls
80 lines (64 loc) · 2.2 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
74
75
76
77
78
79
80
import sys, os
from PIL import Image
currentImage = None
def _openimage(filename):
pathname = filename
if "\\" not in filename or "/" not in filename:
pathname = os.path.dirname(os.path.abspath(__file__)) + "/" + filename
if os.path.exists(pathname):
return Image.open(pathname)
else:
print("File not found!")
exit(-1)
def _saveimage(image):
path = currentImage.filename.split('/')
name = "/".join(path[0:-1]) + "/preview_" + path[-1]
if "\\" not in filename or "/" not in filename:
pathname = os.path.dirname(os.path.abspath(__file__)) + "/preview_" + filename
image.save(pathname)
# image.show()
def squeezeHeight(height):
size = currentImage.size
squeezeScale(height / size[1])
def squeezeWidth(width):
size = currentImage.size
squeezeScale(width / size[0])
def squeezeWidthAndHeight(width, height):
_saveimage(currentImage.resize((width, height)))
def squeezeScale(scale):
size = currentImage.size
_saveimage(currentImage.resize((int(size[0] * scale), int(size[1] * scale))))
if __name__ == "__main__":
args = sys.argv
if ("-h" in args): print("""
Flags: \n-h for help,
-f for the file name,
-sH for setting height,
-sW for setting width,
-sS for setting scale
Example: *** -f filename.jpg -sS 0.5
""")
try:
if "-f" in args:
filename = args[args.index("-f") + 1]
currentImage = _openimage(filename)
if "-sH" in args and "-sW" in args:
width = int(args[args.index("-sW") + 1])
height = int(args[args.index("-sH") + 1])
squeezeWidthAndHeight(width=width, height=height)
exit()
if "-sH" in args:
height = int(args[args.index("-sH") + 1])
squeezeHeight(height=height)
exit()
if "-sW" in args:
width = int(args[args.index("-sW") + 1])
squeezeWidth(width=width)
exit()
if "-sS" in args:
scale = float(args[args.index("-sS") + 1])
squeezeScale(scale=scale)
exit()
except Exception as e:
print("[ERROR]: " + str(e))
print("Entry params are incorrect! Please use -h for getting help.")