2021-08-01 23:21:27 +00:00
|
|
|
'''
|
|
|
|
resize
|
|
|
|
======
|
|
|
|
|
|
|
|
Resize image files.
|
|
|
|
|
|
|
|
pattern:
|
|
|
|
Glob pattern for input files.
|
|
|
|
|
|
|
|
new_w,
|
|
|
|
new_h:
|
|
|
|
New dimensions for the image. If either of these is 0, then that
|
|
|
|
dimension will be calculated by resizing the other side while keeping the
|
|
|
|
aspect ratio.
|
|
|
|
|
|
|
|
--inplace:
|
|
|
|
Overwrite the input files, instead of creating _WxH names.
|
|
|
|
|
|
|
|
--nearest:
|
|
|
|
Use nearest-neighbor scaling to preserve pixelated images.
|
|
|
|
|
|
|
|
--only_shrink:
|
|
|
|
If the input image is smaller than the requested dimensions, do nothing.
|
|
|
|
Useful when globbing in a directory with many differently sized images.
|
|
|
|
|
|
|
|
--scale X:
|
|
|
|
Use this option instead of new_w, new_h. Scale the image by factor X.
|
|
|
|
|
|
|
|
--quality X:
|
|
|
|
JPEG compression quality.
|
|
|
|
'''
|
2020-07-01 03:27:35 +00:00
|
|
|
import argparse
|
2021-06-04 20:13:11 +00:00
|
|
|
import PIL.Image
|
2019-06-12 05:41:31 +00:00
|
|
|
import sys
|
|
|
|
|
2021-08-01 23:21:27 +00:00
|
|
|
from voussoirkit import betterhelp
|
2020-02-05 03:34:39 +00:00
|
|
|
from voussoirkit import imagetools
|
2021-03-03 20:39:39 +00:00
|
|
|
from voussoirkit import pathclass
|
2021-06-04 03:43:27 +00:00
|
|
|
from voussoirkit import pipeable
|
|
|
|
from voussoirkit import vlogging
|
|
|
|
|
|
|
|
log = vlogging.getLogger(__name__, 'resize')
|
2020-01-16 06:47:26 +00:00
|
|
|
|
2020-07-08 05:09:48 +00:00
|
|
|
def resize(
|
|
|
|
filename,
|
2021-08-01 23:21:04 +00:00
|
|
|
new_w=None,
|
|
|
|
new_h=None,
|
2020-07-08 05:09:48 +00:00
|
|
|
*,
|
|
|
|
inplace=False,
|
|
|
|
nearest_neighbor=False,
|
2020-10-23 14:48:31 +00:00
|
|
|
only_shrink=False,
|
2020-07-08 05:09:48 +00:00
|
|
|
scale=None,
|
2021-03-14 04:32:43 +00:00
|
|
|
quality=100,
|
2020-07-08 05:09:48 +00:00
|
|
|
):
|
2021-03-03 20:39:39 +00:00
|
|
|
file = pathclass.Path(filename)
|
2021-06-04 20:13:11 +00:00
|
|
|
image = PIL.Image.open(file.absolute_path)
|
2019-06-12 05:41:31 +00:00
|
|
|
|
2021-06-04 03:43:27 +00:00
|
|
|
(image_width, image_height) = image.size
|
2019-06-12 05:41:31 +00:00
|
|
|
|
2021-08-01 23:21:04 +00:00
|
|
|
if new_w is not None and new_h is not None:
|
2020-07-01 03:27:35 +00:00
|
|
|
pass
|
|
|
|
elif scale:
|
2021-08-01 23:21:04 +00:00
|
|
|
new_w = int(image_width * scale)
|
|
|
|
new_h = int(image_height * scale)
|
2020-07-01 03:27:35 +00:00
|
|
|
|
2021-08-01 23:21:04 +00:00
|
|
|
if new_w == 0:
|
|
|
|
(new_w, new_h) = imagetools.fit_into_bounds(
|
2020-10-23 14:48:31 +00:00
|
|
|
image_width,
|
|
|
|
image_height,
|
|
|
|
10000000,
|
2021-08-01 23:21:04 +00:00
|
|
|
new_h,
|
2020-10-23 14:48:31 +00:00
|
|
|
only_shrink=only_shrink,
|
|
|
|
)
|
2021-08-01 23:21:04 +00:00
|
|
|
if new_h == 0:
|
|
|
|
(new_w, new_h) = imagetools.fit_into_bounds(
|
2020-10-23 14:48:31 +00:00
|
|
|
image_width,
|
|
|
|
image_height,
|
2021-08-01 23:21:04 +00:00
|
|
|
new_w,
|
2020-10-23 14:48:31 +00:00
|
|
|
10000000,
|
|
|
|
only_shrink=only_shrink,
|
|
|
|
)
|
2019-06-12 05:41:31 +00:00
|
|
|
|
2021-08-01 23:21:04 +00:00
|
|
|
log.debug('Resizing %s to %dx%d.', file.absolute_path, new_w, new_h)
|
2020-07-01 03:27:35 +00:00
|
|
|
if nearest_neighbor:
|
2021-08-01 23:21:04 +00:00
|
|
|
image = image.resize( (new_w, new_h), PIL.Image.NEAREST)
|
2020-07-01 03:27:35 +00:00
|
|
|
else:
|
2021-08-01 23:21:04 +00:00
|
|
|
image = image.resize( (new_w, new_h), PIL.Image.ANTIALIAS)
|
2020-07-08 05:09:48 +00:00
|
|
|
|
|
|
|
if inplace:
|
2021-03-03 20:39:39 +00:00
|
|
|
new_name = file
|
2020-07-08 05:09:48 +00:00
|
|
|
else:
|
2021-08-01 23:21:04 +00:00
|
|
|
suffix = '_{width}x{height}'.format(width=new_w, height=new_h)
|
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':
|
2021-06-04 03:43:27 +00:00
|
|
|
image = image.convert('RGB')
|
2020-07-01 03:27:35 +00:00
|
|
|
|
2021-09-23 03:26:53 +00:00
|
|
|
pipeable.stdout(new_name.absolute_path)
|
2021-06-04 03:43:27 +00:00
|
|
|
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):
|
2021-09-24 06:42:34 +00:00
|
|
|
patterns = pipeable.input(args.pattern, skip_blank=True, strip=True)
|
2021-11-08 19:38:10 +00:00
|
|
|
files = pathclass.glob_many_files(patterns)
|
2021-09-24 06:42:34 +00:00
|
|
|
for file in files:
|
2020-07-01 03:27:35 +00:00
|
|
|
resize(
|
2021-09-24 06:42:34 +00:00
|
|
|
file,
|
2021-08-01 23:21:04 +00:00
|
|
|
args.new_w,
|
|
|
|
args.new_h,
|
2020-07-08 05:09:48 +00:00
|
|
|
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
|
|
|
)
|
|
|
|
|
2021-09-24 06:42:34 +00:00
|
|
|
return 0
|
|
|
|
|
2021-06-22 05:11:19 +00:00
|
|
|
@vlogging.main_decorator
|
2020-07-01 03:27:35 +00:00
|
|
|
def main(argv):
|
|
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
|
|
|
|
|
|
parser.add_argument('pattern')
|
2021-08-01 23:21:04 +00:00
|
|
|
parser.add_argument('new_w', nargs='?', type=int, default=None)
|
|
|
|
parser.add_argument('new_h', nargs='?', type=int, default=None)
|
2021-02-21 05:01:55 +00:00
|
|
|
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')
|
2021-02-21 05:01:55 +00:00
|
|
|
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)
|
|
|
|
|
2021-08-01 23:21:27 +00:00
|
|
|
return betterhelp.single_main(argv, parser, __doc__)
|
2020-07-01 03:27:35 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
raise SystemExit(main(sys.argv[1:]))
|