Browse Source

Добавлен ресайзинг методом crop

master
Alexander Demidov 11 years ago
parent
commit
ecaa18524e
  1. BIN
      crop.jpg
  2. BIN
      origin.jpg
  3. 14
      resize.py
  4. BIN
      thumbnail.jpg

BIN
crop.jpg

After

Width: 100  |  Height: 100  |  Size: 13 KiB

BIN
origin.jpg

After

Width: 3264  |  Height: 2448  |  Size: 4.9 MiB

14
resize.py

@ -6,6 +6,7 @@ import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--width", dest="width", 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("source", type=str)
parser.add_argument("destination", type=str)
args = parser.parse_args()
@ -16,6 +17,19 @@ outfile = args.destination
try:
im = Image.open(infile)
im = im.convert()
if args.crop:
width_crop = im.size[0]
height_crop = im.size[0]*(args.width/args.height)
width_padding = 0
height_padding = (im.size[1]-height_crop)/2
if height_crop > im.size[1]:
width_crop = im.size[1]*(args.height/args.width)
height_crop = im.size[1]
width_padding = (im.size[0]-width_crop)/2
height_padding = 0
im_crop = im.crop((width_padding, height_padding, width_crop+width_padding, height_crop+height_padding))
im_result = im_crop.resize(size, Image.ANTIALIAS)
else:
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))

BIN
thumbnail.jpg

After

Width: 100  |  Height: 100  |  Size: 10 KiB

Loading…
Cancel
Save