Browse Source

Добавлен аттрибут в параметрах комнадной строки --keep-aspect-ratio

master
Alexander Demidov 11 years ago
parent
commit
29f9bd4ae6
  1. 8
      resize.py

8
resize.py

@ -8,6 +8,7 @@ parser = argparse.ArgumentParser()
parser.add_argument("--width", dest="width", type=int, required=True) parser.add_argument("--width", dest="width", type=int, required=True)
parser.add_argument("--height", dest="height", type=int, required=True) parser.add_argument("--height", dest="height", type=int, required=True)
parser.add_argument("--crop", action="store_true", required=False) parser.add_argument("--crop", action="store_true", required=False)
parser.add_argument("--keep-aspect-ratio", dest="keep_aspect_ratio", action="store_true", required=False)
parser.add_argument("source", type=str) parser.add_argument("source", type=str)
parser.add_argument("destination", type=str) parser.add_argument("destination", type=str)
args = parser.parse_args() args = parser.parse_args()
@ -33,8 +34,11 @@ try:
im_result = im_crop.resize(size, Image.ANTIALIAS) im_result = im_crop.resize(size, Image.ANTIALIAS)
else: else:
im.thumbnail(size, Image.ANTIALIAS) im.thumbnail(size, Image.ANTIALIAS)
im_result = Image.new('RGB', (args.width, args.height), (255, 255, 255))
im_result.paste(im, ((args.width - im.size[0]) / 2, (args.height - im.size[1]) / 2))
if args.keep_aspect_ratio:
im_result = im
else:
im_result = Image.new('RGB', (args.width, args.height), (255, 255, 255))
im_result.paste(im, ((args.width - im.size[0]) / 2, (args.height - im.size[1]) / 2))
enhancer = ImageEnhance.Contrast(im_result) enhancer = ImageEnhance.Contrast(im_result)
im_result = enhancer.enhance(1.05) im_result = enhancer.enhance(1.05)
im_result.save(outfile, "JPEG", quality=100) im_result.save(outfile, "JPEG", quality=100)

Loading…
Cancel
Save