Check for known extensions after formatting instead of before.

Also add {filename} format variable.
This commit is contained in:
voussoir 2022-01-12 21:31:54 -08:00
parent 5161d28c31
commit 49c23ef4a9
No known key found for this signature in database
GPG key ID: 5F7554F8C26DACCB

View file

@ -27,6 +27,7 @@ flags:
A string that controls the output filename format. Suppose the input file A string that controls the output filename format. Suppose the input file
was myphoto.jpg. You can use these variables in your format string: was myphoto.jpg. You can use these variables in your format string:
{base} = myphoto {base} = myphoto
{filename} = myphoto.jpg
{width} = an integer {width} = an integer
{height} = an integer {height} = an integer
{extension} = .jpg {extension} = .jpg
@ -88,7 +89,6 @@ def resize_core(
def resize( def resize(
filename, filename,
*, *,
destination=None,
output_format=DEFAULT_OUTPUT_FORMAT, output_format=DEFAULT_OUTPUT_FORMAT,
height=None, height=None,
keep_aspect_ratio=True, keep_aspect_ratio=True,
@ -155,20 +155,21 @@ def resize(
output_folder.assert_is_directory() output_folder.assert_is_directory()
base = file.replace_extension('').basename filename = file.basename
if '{extension}' not in output_format:
known_extensions = PIL.Image.registered_extensions()
known_extensions = {os.path.normcase(ext) for ext in known_extensions}
output_norm = os.path.normcase(output_format)
if not any(output_norm.endswith(ext) for ext in known_extensions):
output_format += '{extension}'
output_file = output_format.format( output_file = output_format.format(
base=base, base=file.replace_extension('').basename,
width=width,
height=height,
extension=file.extension.with_dot, extension=file.extension.with_dot,
filename=file.basename,
height=height,
width=width,
) )
output_file = output_folder.with_child(output_file) output_file = output_folder.with_child(output_file)
known_extensions = {os.path.normcase(ext) for ext in PIL.Image.registered_extensions()}
output_norm = output_file.normcase
if not any(output_norm.endswith(ext) for ext in known_extensions):
output_file = output_file.add_extension(file.extension)
if output_file == file: if output_file == file:
raise ValueError('Cannot overwrite input file without OUTPUT_INPLACE.') raise ValueError('Cannot overwrite input file without OUTPUT_INPLACE.')
@ -219,7 +220,6 @@ def main(argv):
parser.add_argument('patterns', nargs='+') parser.add_argument('patterns', nargs='+')
parser.add_argument('--width', type=int, default=None) parser.add_argument('--width', type=int, default=None)
parser.add_argument('--height', type=int, default=None) parser.add_argument('--height', type=int, default=None)
parser.add_argument('--destination', 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')