Update some image scripts to preserve exif.

This commit is contained in:
voussoir 2021-06-03 20:43:27 -07:00
parent 7cfa47b279
commit c01a7c9d9d
No known key found for this signature in database
GPG key ID: 5F7554F8C26DACCB
4 changed files with 33 additions and 25 deletions

23
crop.py
View file

@ -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)

View file

@ -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)

View file

@ -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()

View file

@ -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')