greyscale.py 653 B

1234567891011121314151617181920
  1. import argparse
  2. from PIL import Image, ImageOps
  3. # Specify two command line arguments
  4. parser = argparse.ArgumentParser(description="Convert to greyscale")
  5. parser.add_argument('input_file', help="Image to convert")
  6. parser.add_argument('output_file', nargs='?', default=None,
  7. help="Output image. Replaces input if not specified")
  8. # Parse arguments, setting output=input if not given
  9. args = parser.parse_args()
  10. if args.output_file is None:
  11. args.output_file = args.input_file
  12. # Convert input to greyscale
  13. with Image.open(args.input_file) as im:
  14. grey = ImageOps.grayscale(im)
  15. # Save converted image
  16. grey.save(args.output_file)