2020-07-01 03:27:35 +00:00
|
|
|
import argparse
|
2019-06-12 05:41:31 +00:00
|
|
|
import os
|
|
|
|
from PIL import Image
|
|
|
|
import sys
|
|
|
|
|
2020-02-05 03:34:39 +00:00
|
|
|
from voussoirkit import imagetools
|
2020-01-16 06:47:26 +00:00
|
|
|
from voussoirkit import winglob
|
|
|
|
|
2019-06-12 05:41:31 +00:00
|
|
|
filenames = sys.argv[1]
|
|
|
|
|
2020-07-01 03:27:35 +00:00
|
|
|
def resize(filename, new_x=None, new_y=None, scale=None, nearest_neighbor=False):
|
2019-06-12 05:41:31 +00:00
|
|
|
i = Image.open(filename)
|
|
|
|
|
|
|
|
(image_width, image_height) = i.size
|
|
|
|
|
2020-07-01 03:27:35 +00:00
|
|
|
if new_x is not None and new_y is not None:
|
|
|
|
pass
|
|
|
|
elif scale:
|
|
|
|
new_x = int(image_width * scale)
|
|
|
|
new_y = int(image_height * scale)
|
|
|
|
print(new_x, new_y)
|
|
|
|
|
2019-06-12 05:41:31 +00:00
|
|
|
if new_x == 0:
|
2020-02-05 03:34:39 +00:00
|
|
|
(new_x, new_y) = imagetools.fit_into_bounds(image_width, image_height, 10000000, new_y)
|
2019-06-12 05:41:31 +00:00
|
|
|
if new_y == 0:
|
2020-02-05 03:34:39 +00:00
|
|
|
(new_x, new_y) = imagetools.fit_into_bounds(image_width, image_height, new_x, 10000000)
|
2019-06-12 05:41:31 +00:00
|
|
|
|
|
|
|
print(i.size, new_x, new_y)
|
2020-07-01 03:27:35 +00:00
|
|
|
if nearest_neighbor:
|
|
|
|
i = i.resize( (new_x, new_y), Image.NEAREST)
|
|
|
|
else:
|
|
|
|
i = i.resize( (new_x, new_y), Image.ANTIALIAS)
|
2019-06-12 05:41:31 +00:00
|
|
|
suffix = '_{width}x{height}'.format(width=new_x, height=new_y)
|
|
|
|
(base, extension) = os.path.splitext(filename)
|
|
|
|
newname = base + suffix + extension
|
|
|
|
i.save(newname, quality=100)
|
2020-07-01 03:27:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
def resize_argparse(args):
|
|
|
|
filenames = winglob.glob(args.pattern)
|
|
|
|
for filename in filenames:
|
|
|
|
resize(
|
|
|
|
filename,
|
|
|
|
args.new_x,
|
|
|
|
args.new_y,
|
|
|
|
scale=args.scale,
|
|
|
|
nearest_neighbor=args.nearest_neighbor,
|
|
|
|
)
|
|
|
|
|
|
|
|
def main(argv):
|
|
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
|
|
|
|
|
|
parser.add_argument('pattern')
|
|
|
|
parser.add_argument('new_x', nargs='?', type=int, default=None)
|
|
|
|
parser.add_argument('new_y', nargs='?', type=int, default=None)
|
|
|
|
parser.add_argument('--scale', dest='scale', type=float, default=None)
|
|
|
|
parser.add_argument('--nearest', dest='nearest_neighbor', action='store_true')
|
|
|
|
parser.set_defaults(func=resize_argparse)
|
|
|
|
|
|
|
|
args = parser.parse_args(argv)
|
|
|
|
return args.func(args)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
raise SystemExit(main(sys.argv[1:]))
|