-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshirt.py
69 lines (45 loc) · 1.47 KB
/
shirt.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
from sys import argv, exit
from PIL import Image, ImageOps
from os.path import splitext
EXTENSIONS = [".jpg", ".jpeg", ".png"]
def main():
# VALIDATE AND STORE COMMAND-LINE ARGUMENTS
input_image, output_image = validate_arguments()
# GET MUPPET IMAGE
muppet = validate_image(input_image)
# GET SHIRT IMAGE AND SIZE
shirt = validate_image("shirt.png")
size = shirt.size
# RESIZE MUPPET IMAGE TO SHIRT.PNG DIMENSIONS
muppet = ImageOps.fit(muppet, size)
# PASTE SHIRT OVER MUPPET
muppet.paste(shirt, shirt)
# SAVE RESULT
muppet.save(output_image)
def validate_arguments() -> tuple:
# VALIDATE 2 COMMAND LINE INPUTS
if len(argv) != 3:
exit("Usage: python shirt.py before.png after.png")
# SPLIT TO ISOLATE EXTENSION
try:
input_split: tuple = splitext(argv[1])
output_split: tuple = splitext(argv[2])
# CHECK FOR VALID EXTENSION TYPES
validate_extension(input_split)
validate_extension(output_split)
if not input_split[1] == output_split[1]:
raise Exception
except:
exit(f"Invalid or Missing Extension")
return argv[1], argv[2]
def validate_extension(arg) -> bool:
if arg[1] not in EXTENSIONS:
raise Exception
def validate_image(image_file):
try:
image = Image.open(image_file)
return image
except FileNotFoundError:
exit("Image does not exist")
if __name__ == "__main__":
main()