You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

26 lines
941 B

11 years ago
11 years ago
  1. #!/usr/bin/env python
  2. import os, sys
  3. from PIL import Image
  4. from PIL import ImageEnhance
  5. import argparse
  6. parser = argparse.ArgumentParser()
  7. parser.add_argument("--width", dest="width", type=int, required=True)
  8. parser.add_argument("--height", dest="height", type=int, required=True)
  9. parser.add_argument("source", type=str)
  10. parser.add_argument("destination", type=str)
  11. args = parser.parse_args()
  12. size = args.width, args.height
  13. infile = args.source
  14. outfile = args.destination
  15. try:
  16. im = Image.open(infile)
  17. im = im.convert()
  18. im.thumbnail(size, Image.ANTIALIAS)
  19. im_result = Image.new('RGB', (args.width, args.height), (255, 255, 255))
  20. im_result.paste(im, ((args.width - im.size[0]) / 2, (args.height - im.size[1]) / 2))
  21. enhancer = ImageEnhance.Contrast(im_result)
  22. im_result = enhancer.enhance(1.05)
  23. im_result.save(outfile, "JPEG", quality=100)
  24. except IOError:
  25. print("cannot create thumbnail for '%s'" % infile)