Queer European MD passionate about IT

shirt.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import os
  2. import sys
  3. import PIL
  4. from PIL import Image
  5. def main():
  6. try:
  7. input_file_name, output_file_name = sys.argv[1:3]
  8. except (IndexError, ValueError):
  9. print("Too few command-line arguments")
  10. sys.exit(1)
  11. if len(sys.argv) > 3:
  12. print("Too many command-line arguments")
  13. sys.exit(1)
  14. if not os.path.isfile(input_file_name): # Or try opening and catch FileNotFoundError
  15. print(f"Input does not exist")
  16. sys.exit(1)
  17. 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"))):
  18. print(f"Invalid input")
  19. sys.exit(1)
  20. 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:]):
  21. print(f"Input and output have different extensions")
  22. sys.exit(1)
  23. overlap_t_shirt(input_file_name, output_file_name)
  24. """Open the input with Image.open, per pillow.
  25. resize and crop the input with ImageOps.fit, per pillow.readthedocs.io/en/stable/reference/ImageOps.html#PIL.ImageOps.fit,
  26. using default values for method, bleed, and centering, overlay the shirt with Image.paste,
  27. per pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.paste,
  28. and save the result with Image.save, per pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.save."""
  29. def overlap_t_shirt(input_file_name, output_file_name):
  30. image = Image.open(input_file_name)
  31. shirt = Image.open('shirt.png')
  32. image = PIL.ImageOps.fit(image, size=shirt.size)
  33. image.paste(shirt, mask=shirt)
  34. image.save(output_file_name)
  35. if __name__ == "__main__":
  36. main()