Preparing git repo for final project
BIN
problems/pset6/shirt/after1.jpg
Normal file
After Width: | Height: | Size: 44 KiB |
BIN
problems/pset6/shirt/after2.jpg
Normal file
After Width: | Height: | Size: 43 KiB |
BIN
problems/pset6/shirt/after3.jpg
Normal file
After Width: | Height: | Size: 42 KiB |
BIN
problems/pset6/shirt/before1.jpg
Normal file
After Width: | Height: | Size: 980 KiB |
BIN
problems/pset6/shirt/before2.jpg
Normal file
After Width: | Height: | Size: 1.2 MiB |
BIN
problems/pset6/shirt/before3.jpg
Normal file
After Width: | Height: | Size: 1.2 MiB |
BIN
problems/pset6/shirt/shirt.png
Normal file
After Width: | Height: | Size: 64 KiB |
43
problems/pset6/shirt/shirt.py
Normal file
@@ -0,0 +1,43 @@
|
||||
import os
|
||||
import sys
|
||||
|
||||
import PIL
|
||||
from PIL import Image
|
||||
|
||||
|
||||
def main():
|
||||
try:
|
||||
input_file_name, output_file_name = sys.argv[1:3]
|
||||
except (IndexError, ValueError):
|
||||
print("Too few command-line arguments")
|
||||
sys.exit(1)
|
||||
if len(sys.argv) > 3:
|
||||
print("Too many command-line arguments")
|
||||
sys.exit(1)
|
||||
if not os.path.isfile(input_file_name): # Or try opening and catch FileNotFoundError
|
||||
print(f"Input does not exist")
|
||||
sys.exit(1)
|
||||
if not (any(input_file_name.lower().endswith(format) for format in (".jpg", ".jpeg", ".png")) and any(output_file_name.lower().endswith(format) for format in (".jpg", ".jpeg", ".png"))):
|
||||
print(f"Invalid input")
|
||||
sys.exit(1)
|
||||
if (input_file_name[-4] == '.' and input_file_name[-4:] != output_file_name[-4:]) or (input_file_name[-3] == '.' and input_file_name[-3:] != output_file_name[-3:]):
|
||||
print(f"Input and output have different extensions")
|
||||
sys.exit(1)
|
||||
overlap_t_shirt(input_file_name, output_file_name)
|
||||
"""Open the input with Image.open, per pillow.
|
||||
resize and crop the input with ImageOps.fit, per pillow.readthedocs.io/en/stable/reference/ImageOps.html#PIL.ImageOps.fit,
|
||||
using default values for method, bleed, and centering, overlay the shirt with Image.paste,
|
||||
per pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.paste,
|
||||
and save the result with Image.save, per pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.save."""
|
||||
|
||||
|
||||
def overlap_t_shirt(input_file_name, output_file_name):
|
||||
image = Image.open(input_file_name)
|
||||
shirt = Image.open('shirt.png')
|
||||
image = PIL.ImageOps.fit(image, size=shirt.size)
|
||||
image.paste(shirt, mask=shirt)
|
||||
image.save(output_file_name)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|