This commit is contained in:
Rehtt 2021-06-22 10:28:29 +08:00
parent 3b8607f130
commit 376beddbca

View file

@ -34,6 +34,8 @@ def main():
help='resize the image to the correct size') help='resize the image to the correct size')
parser.add_argument('--keep-aspect-ratio', dest='keep_aspect_ratio', action='store_true', parser.add_argument('--keep-aspect-ratio', dest='keep_aspect_ratio', action='store_true',
help='keep the original aspect ratio when resizing (and cut off excess pixels)') help='keep the original aspect ratio when resizing (and cut off excess pixels)')
parser.add_argument('--keep-aspect', dest='keep-aspect', action='store_true',
help='keep the original aspect ratio when resizing (not cut off excess pixels)')
parser.add_argument('--resample', dest='resample', default='lanczos', parser.add_argument('--resample', dest='resample', default='lanczos',
choices=('nearest', 'bicubic', 'lanczos'), choices=('nearest', 'bicubic', 'lanczos'),
help='which resampling filter to use for resizing (see Pillow documentation)') help='which resampling filter to use for resizing (see Pillow documentation)')
@ -42,14 +44,17 @@ def main():
mode = module_map[args.mode] mode = module_map[args.mode]
if args.resize and any(i != m for i, m in zip(image.size, (mode.WIDTH, mode.HEIGHT))): if args.resize and any(i != m for i, m in zip(image.size, (mode.WIDTH, mode.HEIGHT))):
resample = getattr(Image, args.resample.upper()) resample = getattr(Image, args.resample.upper())
if args.keep_aspect_ratio: if args.keep_aspect_ratio or args.keep_aspect:
orig_ratio = image.width / image.height orig_ratio = image.width / image.height
mode_ratio = mode.WIDTH / mode.HEIGHT mode_ratio = mode.WIDTH / mode.HEIGHT
crop = orig_ratio != mode_ratio crop = orig_ratio != mode_ratio
else: else:
crop = False crop = False
if crop: if crop:
if orig_ratio < mode_ratio: t = orig_ratio < mode_ratio
if args.keep_aspect:
t = orig_ratio > mode_ratio
if t:
w = mode.WIDTH w = mode.WIDTH
h = int(w / orig_ratio) h = int(w / orig_ratio)
else: else:
@ -59,6 +64,13 @@ def main():
w = mode.WIDTH w = mode.WIDTH
h = mode.HEIGHT h = mode.HEIGHT
image = image.resize((w, h), resample) image = image.resize((w, h), resample)
if args.keep_aspect:
newbg = Image.new('RGB', (mode.WIDTH, mode.HEIGHT))
if t:
newbg.paste(image, (0, (mode.HEIGHT/2)-(h/2)))
else:
newbg.paste(image, ((mode.WIDTH/2)-(w/2), 0))
image = newbg.copy()
if crop: if crop:
x = (image.width - mode.WIDTH) / 2 x = (image.width - mode.WIDTH) / 2
y = (image.height - mode.HEIGHT) / 2 y = (image.height - mode.HEIGHT) / 2