Skip to content

Commit e103397

Browse files
committed
problem set 6, exercise 4
1 parent 2cf0f49 commit e103397

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed
Loading
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# https://pillow.readthedocs.io/en/stable/handbook/
2+
# https://pillow.readthedocs.io/en/stable/reference/ImageOps.html#PIL.ImageOps.fit
3+
# https://pillow.readthedocs.io/en/stable/reference/Image.html
4+
5+
import sys
6+
from os.path import isfile
7+
from PIL import Image, ImageOps
8+
9+
10+
def main():
11+
if len(sys.argv) != 3:
12+
sys.exit(f"Usage: python {sys.argv[0]} <image1> <image2>")
13+
elif not isfile(sys.argv[1]):
14+
sys.exit(f"Error: {sys.argv[1]} does not exist")
15+
elif not sys.argv[1].lower().endswith((".png", ".jpg", "jpg")) or not sys.argv[
16+
2
17+
].lower().endswith((".png", ".jpg", "jpg")):
18+
sys.exit("Error: invalid extension. File must be: .png, .jpg, or .jpeg")
19+
elif sys.argv[1].split(".")[-1] != sys.argv[2].split(".")[-1]:
20+
# Another possibility is os.path.splitext(path) that returns a tuple (path, extension)
21+
sys.exit(
22+
"Error: both files must have the same extension (.png, .jpg, or .jpeg)"
23+
)
24+
25+
overlap_shirt(sys.argv[1], sys.argv[2])
26+
27+
28+
def overlap_shirt(src, dst):
29+
with Image.open("./shirt.png") as shirt:
30+
shirt_size = shirt.size # tuple containing (width, height)
31+
with Image.open(src) as photo:
32+
# the shirt is "smaller" than the photo. Hence, we resize the photo
33+
# to make the shirt fit on it
34+
shirt_on_photo = ImageOps.fit(photo, shirt_size)
35+
shirt_on_photo.paste(shirt, shirt) # paste the shirt on the resized photo
36+
shirt_on_photo.save(dst)
37+
38+
39+
if __name__ == "__main__":
40+
main()

0 commit comments

Comments
 (0)