cmd/resize.py

107 lines
3 KiB
Python
Raw Normal View History

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
from voussoirkit import imagetools
2021-03-03 20:39:39 +00:00
from voussoirkit import pathclass
from voussoirkit import pipeable
from voussoirkit import winglob
from voussoirkit import vlogging
log = vlogging.getLogger(__name__, 'resize')
def resize(
filename,
new_x=None,
new_y=None,
*,
inplace=False,
nearest_neighbor=False,
2020-10-23 14:48:31 +00:00
only_shrink=False,
scale=None,
2021-03-14 04:32:43 +00:00
quality=100,
):
2021-03-03 20:39:39 +00:00
file = pathclass.Path(filename)
image = Image.open(file.absolute_path)
2019-06-12 05:41:31 +00:00
(image_width, image_height) = image.size
2019-06-12 05:41:31 +00:00
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)
2019-06-12 05:41:31 +00:00
if new_x == 0:
2020-10-23 14:48:31 +00:00
(new_x, new_y) = imagetools.fit_into_bounds(
image_width,
image_height,
10000000,
new_y,
only_shrink=only_shrink,
)
2019-06-12 05:41:31 +00:00
if new_y == 0:
2020-10-23 14:48:31 +00:00
(new_x, new_y) = imagetools.fit_into_bounds(
image_width,
image_height,
new_x,
10000000,
only_shrink=only_shrink,
)
2019-06-12 05:41:31 +00:00
log.debug('Resizing %s to %dx%d.', file.absolute_path, new_x, new_y)
2020-07-01 03:27:35 +00:00
if nearest_neighbor:
image = image.resize( (new_x, new_y), Image.NEAREST)
2020-07-01 03:27:35 +00:00
else:
image = image.resize( (new_x, new_y), Image.ANTIALIAS)
if inplace:
2021-03-03 20:39:39 +00:00
new_name = file
else:
suffix = '_{width}x{height}'.format(width=new_x, height=new_y)
2021-03-03 20:39:39 +00:00
base = file.replace_extension('').basename
new_name = base + suffix + file.extension.with_dot
new_name = file.parent.with_child(new_name)
if new_name.extension == '.jpg':
image = image.convert('RGB')
2020-07-01 03:27:35 +00:00
pipeable.output(new_name.absolute_path)
image.save(new_name.absolute_path, exif=image.info.get('exif', b''), quality=quality)
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,
inplace=args.inplace,
2020-10-23 14:48:13 +00:00
nearest_neighbor=args.nearest_neighbor,
2020-10-23 14:48:31 +00:00
only_shrink=args.only_shrink,
2020-10-23 14:48:13 +00:00
scale=args.scale,
2021-03-14 04:32:43 +00:00
quality=args.quality,
2020-07-01 03:27:35 +00:00
)
def main(argv):
argv = vlogging.main_level_by_argv(argv)
2020-07-01 03:27:35 +00:00
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('--inplace', action='store_true')
2020-10-23 14:48:13 +00:00
parser.add_argument('--nearest', dest='nearest_neighbor', action='store_true')
parser.add_argument('--only_shrink', '--only-shrink', action='store_true')
parser.add_argument('--scale', type=float, default=None)
2021-03-14 04:32:43 +00:00
parser.add_argument('--quality', type=int, default=100)
2020-07-01 03:27:35 +00:00
parser.set_defaults(func=resize_argparse)
args = parser.parse_args(argv)
return args.func(args)
if __name__ == '__main__':
raise SystemExit(main(sys.argv[1:]))