From c01a7c9d9dd6e3bf10cd1270b158181ffa102303 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Thu, 3 Jun 2021 20:43:27 -0700 Subject: [PATCH] Update some image scripts to preserve exif. --- crop.py | 23 ++++++++++++----------- grayscale.py | 7 ++++--- rejpg.py | 6 +++--- resize.py | 22 ++++++++++++++-------- 4 files changed, 33 insertions(+), 25 deletions(-) diff --git a/crop.py b/crop.py index 5efc5bc..ac1eee5 100644 --- a/crop.py +++ b/crop.py @@ -3,22 +3,22 @@ import os from PIL import Image import sys +from voussoirkit import pipeable from voussoirkit import winglob def crop(filename, crops, *, inplace=False): - print(crops) - i = Image.open(filename) + image = Image.open(filename) if len(crops) == 2: - crops.extend(i.size) + crops.extend(image.size) - if crops[0] < 0: crops[0] = i.size[0] + crops[0] - if crops[1] < 0: crops[1] = i.size[1] + crops[1] - if crops[2] < 0: crops[2] = i.size[0] + crops[2] - if crops[3] < 0: crops[3] = i.size[1] + crops[3] - if crops[2] == 0: crops[2] = i.size[0] - if crops[3] == 0: crops[3] = i.size[1] + if crops[0] < 0: crops[0] = image.size[0] + crops[0] + if crops[1] < 0: crops[1] = image.size[1] + crops[1] + if crops[2] < 0: crops[2] = image.size[0] + crops[2] + if crops[3] < 0: crops[3] = image.size[1] + crops[3] + if crops[2] == 0: crops[2] = image.size[0] + if crops[3] == 0: crops[3] = image.size[1] - i = i.crop(crops) + image = image.crop(crops) if inplace: newname = filename else: @@ -26,8 +26,9 @@ def crop(filename, crops, *, inplace=False): suffix = f'_{suffix}' (base, extension) = os.path.splitext(filename) newname = base + suffix + extension - i.save(newname, quality=100) + pipeable.stdout(newname) + image.save(newname, exif=image.info.get('exif', b''), quality=100) def crop_argparse(args): filenames = winglob.glob(args.pattern) diff --git a/grayscale.py b/grayscale.py index 47f690e..0f32514 100644 --- a/grayscale.py +++ b/grayscale.py @@ -18,9 +18,10 @@ def grayscale(filename, *, inplace=False): basename += '_gray' new_filename = filename.parent.with_child(basename).add_extension(filename.extension) - image = PIL.Image.open(filename.absolute_path).convert('LA') - print(f'{new_filename.relative_path}') - image.save(new_filename.absolute_path) + image = PIL.Image.open(filename.absolute_path) + image = image.convert('LA').convert(image.mode) + print(f'{new_filename.absolute_path}') + image.save(new_filename.absolute_path, exif=image.info.get('exif', b'')) def grayscale_argparse(args): filenames = winglob.glob(args.pattern) diff --git a/rejpg.py b/rejpg.py index 1161af0..461da5a 100644 --- a/rejpg.py +++ b/rejpg.py @@ -27,11 +27,11 @@ def rejpg_argparse(args): for filename in files: print(''.join(c for c in filename if c in string.printable)) bytesio = io.BytesIO() - i = PIL.Image.open(filename) + image = PIL.Image.open(filename) - i = imagetools.rotate_by_exif(i) + image = imagetools.rotate_by_exif(image) - i.save(bytesio, format='jpeg', quality=args.quality) + image.save(bytesio, format='jpeg', exif=image.info.get('exif', b''), quality=args.quality) bytesio.seek(0) new_bytes = bytesio.read() diff --git a/resize.py b/resize.py index 83b61be..9e69001 100644 --- a/resize.py +++ b/resize.py @@ -5,7 +5,11 @@ import sys from voussoirkit import imagetools from voussoirkit import pathclass +from voussoirkit import pipeable from voussoirkit import winglob +from voussoirkit import vlogging + +log = vlogging.getLogger(__name__, 'resize') def resize( filename, @@ -19,9 +23,9 @@ def resize( quality=100, ): file = pathclass.Path(filename) - i = Image.open(file.absolute_path) + image = Image.open(file.absolute_path) - (image_width, image_height) = i.size + (image_width, image_height) = image.size if new_x is not None and new_y is not None: pass @@ -46,11 +50,11 @@ def resize( only_shrink=only_shrink, ) - print(i.size, new_x, new_y) + log.debug('Resizing %s to %dx%d.', file.absolute_path, new_x, new_y) if nearest_neighbor: - i = i.resize( (new_x, new_y), Image.NEAREST) + image = image.resize( (new_x, new_y), Image.NEAREST) else: - i = i.resize( (new_x, new_y), Image.ANTIALIAS) + image = image.resize( (new_x, new_y), Image.ANTIALIAS) if inplace: new_name = file @@ -61,10 +65,10 @@ def resize( new_name = file.parent.with_child(new_name) if new_name.extension == '.jpg': - i = i.convert('RGB') - - i.save(new_name.absolute_path, quality=quality) + image = image.convert('RGB') + pipeable.output(new_name.absolute_path) + image.save(new_name.absolute_path, exif=image.info.get('exif', b''), quality=quality) def resize_argparse(args): filenames = winglob.glob(args.pattern) @@ -81,6 +85,8 @@ def resize_argparse(args): ) def main(argv): + argv = vlogging.main_level_by_argv(argv) + parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('pattern')