Let resize take --destination.

This commit is contained in:
voussoir 2021-11-14 22:00:45 -08:00
parent eefbc3f8aa
commit c8e5a53bcb
No known key found for this signature in database
GPG key ID: 5F7554F8C26DACCB

View file

@ -16,6 +16,10 @@ new_h:
aspect ratio. aspect ratio.
flags: flags:
--destination:
A path to a directory where the png files should be saved. By default,
they go to the same folder as the input file.
--inplace: --inplace:
Overwrite the input files, instead of creating _WxH names. Overwrite the input files, instead of creating _WxH names.
@ -49,6 +53,7 @@ def resize(
new_w=None, new_w=None,
new_h=None, new_h=None,
*, *,
destination=None,
inplace=False, inplace=False,
nearest_neighbor=False, nearest_neighbor=False,
only_shrink=False, only_shrink=False,
@ -89,13 +94,19 @@ def resize(
else: else:
image = image.resize( (new_w, new_h), PIL.Image.ANTIALIAS) image = image.resize( (new_w, new_h), PIL.Image.ANTIALIAS)
if destination is None:
destination = file.parent
else:
destination = pathclass.Path(destination)
destination.assert_is_directory()
if inplace: if inplace:
new_name = file new_name = destination.with_child(file.basename)
else: else:
suffix = '_{width}x{height}'.format(width=new_w, height=new_h) suffix = '_{width}x{height}'.format(width=new_w, height=new_h)
base = file.replace_extension('').basename base = file.replace_extension('').basename
new_name = base + suffix + file.extension.with_dot new_name = base + suffix + file.extension.with_dot
new_name = file.parent.with_child(new_name) new_name = destination.with_child(new_name)
if new_name.extension == '.jpg': if new_name.extension == '.jpg':
image = image.convert('RGB') image = image.convert('RGB')
@ -111,6 +122,7 @@ def resize_argparse(args):
file, file,
args.new_w, args.new_w,
args.new_h, args.new_h,
destination=args.destination,
inplace=args.inplace, inplace=args.inplace,
nearest_neighbor=args.nearest_neighbor, nearest_neighbor=args.nearest_neighbor,
only_shrink=args.only_shrink, only_shrink=args.only_shrink,
@ -127,6 +139,7 @@ def main(argv):
parser.add_argument('pattern') parser.add_argument('pattern')
parser.add_argument('new_w', nargs='?', type=int, default=None) parser.add_argument('new_w', nargs='?', type=int, default=None)
parser.add_argument('new_h', nargs='?', type=int, default=None) parser.add_argument('new_h', nargs='?', type=int, default=None)
parser.add_argument('--destination', nargs='?', default=None)
parser.add_argument('--inplace', action='store_true') parser.add_argument('--inplace', action='store_true')
parser.add_argument('--nearest', dest='nearest_neighbor', action='store_true') parser.add_argument('--nearest', dest='nearest_neighbor', action='store_true')
parser.add_argument('--only_shrink', '--only-shrink', action='store_true') parser.add_argument('--only_shrink', '--only-shrink', action='store_true')