-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdmsn
More file actions
executable file
·67 lines (51 loc) · 1.64 KB
/
dmsn
File metadata and controls
executable file
·67 lines (51 loc) · 1.64 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
#!/usr/bin/env python3
# enter an image and percentage to scale by and print the result
# requires pillow
import sys
from PIL import Image, UnidentifiedImageError
def main() -> int:
"""perform the main operations"""
if len(sys.argv) != 3 or ("-h", "--help") in sys.argv:
usage()
image: str = sys.argv[1]
try:
percent: float = float(sys.argv[2].strip("%"))
except ValueError:
print(f"Invalid value for percent: {sys.argv[2]}. Value must be a number.\n")
usage(1)
dimensions: dict = get_dimensions(image)
dimensions = resize_dimensions(dimensions, percent * 0.01)
print(f"{dimensions['width']} x {dimensions['height']}")
return 0
def get_dimensions(image: str) -> dict:
"""get the dimensions of {image}"""
try:
img: Image = Image.open(image)
except FileNotFoundError as err:
print(err)
sys.exit(1)
except UnidentifiedImageError:
print(f"{image} is not an image.")
sys.exit(1)
width, height = img.size
return {"width": width, "height": height}
def resize_dimensions(dimensions, scaling) -> dict:
"""return the new dimensions of an image"""
for dim in dimensions.keys():
dimensions[dim] *= scaling
return dimensions
def usage(exit_code=0):
"""print usage information"""
print(
"""
usage: dmsn [-h] image percent
positional arguments:
image path to an image to get the dimensions of (string)
optional arguments:
percent percentage to scale the image by (number)
-h, --help show this help message and exit
""".strip()
)
sys.exit(exit_code)
if __name__ == "__main__":
main()