12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- 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()
|