decode cleanup

This commit is contained in:
Andrew Cassidy 2021-04-09 02:19:58 -07:00
parent 156880d430
commit 8a85f5c920

View File

@ -7,15 +7,15 @@ from pathlib import Path
@click.command()
@click.option('-f', '--flip', help="vertically flip image after converting")
@click.option('-r', '--remove', help="remove input images after converting")
@click.option('-s', '--suffix', type=str, default='', help="suffix to append to output file(s).")
@click.option('-f/-F', '--flip/--no-flip', default=True, show_default=True, help="Vertically flip image after converting.")
@click.option('-r', '--remove', is_flag=True, help="Remove input images after converting.")
@click.option('-s', '--suffix', type=str, default='', help="Suffix to append to output file(s). Ignored if output is a single file.")
@click.option('-x', '--extension',
type=str, default='png',
help="extension to use for output. ignored if output is a single file. output filetype is deduced from this")
type=str, default='png', show_default=True,
help="Extension to use for output. Ignored if output is a single file. Output filetype is deduced from this")
@click.option('-o', '--output',
type=click.Path(exists=True, readable=True), default='.',
help="output file name or directory. If outputting to a file, input filenames must be only a single item.")
type=click.Path(exists=True, readable=True), default=None,
help="Output file name or directory. If outputting to a file, input filenames must be only a single item. By default, files are decoded in place.")
@click.argument('filenames',
nargs=-1,
type=click.Path(exists=True, readable=True))
@ -23,16 +23,23 @@ def decode(flip, remove, suffix, extension, output, filenames):
"""Decode an input texture file to an image"""
assert len(filenames) != 0, 'No input files provided.'
outpath = Path(output)
if outpath.is_file():
assert len(filenames) == 1, 'Provided an output file with multiple inputs.'
if output:
outpath = Path(output)
if outpath.is_file():
# decode to a file
assert len(filenames) == 1, 'Provided an output file with multiple inputs.'
def make_outpath(p):
return outpath
def make_outpath(p):
return outpath
else:
# decode to directory
def make_outpath(p):
return outpath / (p.stem + suffix + '.' + extension)
else:
# decode in place
def make_outpath(p):
return outpath / (p.stem + suffix + '.' + extension)
return p.with_name(p.stem + suffix + '.' + extension)
for filename in filenames:
filepath = Path(filename)
@ -43,4 +50,11 @@ def decode(flip, remove, suffix, extension, output, filenames):
ddsfile = dds.read(filepath)
image = ddsfile.decode()
if flip:
image = image.transpose(Image.FLIP_TOP_BOTTOM)
image.save(outpath)
if remove:
os.remove(filepath)