Update some image scripts to preserve exif.
This commit is contained in:
parent
7cfa47b279
commit
c01a7c9d9d
4 changed files with 33 additions and 25 deletions
23
crop.py
23
crop.py
|
@ -3,22 +3,22 @@ import os
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
from voussoirkit import pipeable
|
||||||
from voussoirkit import winglob
|
from voussoirkit import winglob
|
||||||
|
|
||||||
def crop(filename, crops, *, inplace=False):
|
def crop(filename, crops, *, inplace=False):
|
||||||
print(crops)
|
image = Image.open(filename)
|
||||||
i = Image.open(filename)
|
|
||||||
if len(crops) == 2:
|
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[0] < 0: crops[0] = image.size[0] + crops[0]
|
||||||
if crops[1] < 0: crops[1] = i.size[1] + crops[1]
|
if crops[1] < 0: crops[1] = image.size[1] + crops[1]
|
||||||
if crops[2] < 0: crops[2] = i.size[0] + crops[2]
|
if crops[2] < 0: crops[2] = image.size[0] + crops[2]
|
||||||
if crops[3] < 0: crops[3] = i.size[1] + crops[3]
|
if crops[3] < 0: crops[3] = image.size[1] + crops[3]
|
||||||
if crops[2] == 0: crops[2] = i.size[0]
|
if crops[2] == 0: crops[2] = image.size[0]
|
||||||
if crops[3] == 0: crops[3] = i.size[1]
|
if crops[3] == 0: crops[3] = image.size[1]
|
||||||
|
|
||||||
i = i.crop(crops)
|
image = image.crop(crops)
|
||||||
if inplace:
|
if inplace:
|
||||||
newname = filename
|
newname = filename
|
||||||
else:
|
else:
|
||||||
|
@ -26,8 +26,9 @@ def crop(filename, crops, *, inplace=False):
|
||||||
suffix = f'_{suffix}'
|
suffix = f'_{suffix}'
|
||||||
(base, extension) = os.path.splitext(filename)
|
(base, extension) = os.path.splitext(filename)
|
||||||
newname = base + suffix + extension
|
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):
|
def crop_argparse(args):
|
||||||
filenames = winglob.glob(args.pattern)
|
filenames = winglob.glob(args.pattern)
|
||||||
|
|
|
@ -18,9 +18,10 @@ def grayscale(filename, *, inplace=False):
|
||||||
basename += '_gray'
|
basename += '_gray'
|
||||||
new_filename = filename.parent.with_child(basename).add_extension(filename.extension)
|
new_filename = filename.parent.with_child(basename).add_extension(filename.extension)
|
||||||
|
|
||||||
image = PIL.Image.open(filename.absolute_path).convert('LA')
|
image = PIL.Image.open(filename.absolute_path)
|
||||||
print(f'{new_filename.relative_path}')
|
image = image.convert('LA').convert(image.mode)
|
||||||
image.save(new_filename.absolute_path)
|
print(f'{new_filename.absolute_path}')
|
||||||
|
image.save(new_filename.absolute_path, exif=image.info.get('exif', b''))
|
||||||
|
|
||||||
def grayscale_argparse(args):
|
def grayscale_argparse(args):
|
||||||
filenames = winglob.glob(args.pattern)
|
filenames = winglob.glob(args.pattern)
|
||||||
|
|
6
rejpg.py
6
rejpg.py
|
@ -27,11 +27,11 @@ def rejpg_argparse(args):
|
||||||
for filename in files:
|
for filename in files:
|
||||||
print(''.join(c for c in filename if c in string.printable))
|
print(''.join(c for c in filename if c in string.printable))
|
||||||
bytesio = io.BytesIO()
|
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)
|
bytesio.seek(0)
|
||||||
new_bytes = bytesio.read()
|
new_bytes = bytesio.read()
|
||||||
|
|
22
resize.py
22
resize.py
|
@ -5,7 +5,11 @@ import sys
|
||||||
|
|
||||||
from voussoirkit import imagetools
|
from voussoirkit import imagetools
|
||||||
from voussoirkit import pathclass
|
from voussoirkit import pathclass
|
||||||
|
from voussoirkit import pipeable
|
||||||
from voussoirkit import winglob
|
from voussoirkit import winglob
|
||||||
|
from voussoirkit import vlogging
|
||||||
|
|
||||||
|
log = vlogging.getLogger(__name__, 'resize')
|
||||||
|
|
||||||
def resize(
|
def resize(
|
||||||
filename,
|
filename,
|
||||||
|
@ -19,9 +23,9 @@ def resize(
|
||||||
quality=100,
|
quality=100,
|
||||||
):
|
):
|
||||||
file = pathclass.Path(filename)
|
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:
|
if new_x is not None and new_y is not None:
|
||||||
pass
|
pass
|
||||||
|
@ -46,11 +50,11 @@ def resize(
|
||||||
only_shrink=only_shrink,
|
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:
|
if nearest_neighbor:
|
||||||
i = i.resize( (new_x, new_y), Image.NEAREST)
|
image = image.resize( (new_x, new_y), Image.NEAREST)
|
||||||
else:
|
else:
|
||||||
i = i.resize( (new_x, new_y), Image.ANTIALIAS)
|
image = image.resize( (new_x, new_y), Image.ANTIALIAS)
|
||||||
|
|
||||||
if inplace:
|
if inplace:
|
||||||
new_name = file
|
new_name = file
|
||||||
|
@ -61,10 +65,10 @@ def resize(
|
||||||
new_name = file.parent.with_child(new_name)
|
new_name = file.parent.with_child(new_name)
|
||||||
|
|
||||||
if new_name.extension == '.jpg':
|
if new_name.extension == '.jpg':
|
||||||
i = i.convert('RGB')
|
image = image.convert('RGB')
|
||||||
|
|
||||||
i.save(new_name.absolute_path, quality=quality)
|
|
||||||
|
|
||||||
|
pipeable.output(new_name.absolute_path)
|
||||||
|
image.save(new_name.absolute_path, exif=image.info.get('exif', b''), quality=quality)
|
||||||
|
|
||||||
def resize_argparse(args):
|
def resize_argparse(args):
|
||||||
filenames = winglob.glob(args.pattern)
|
filenames = winglob.glob(args.pattern)
|
||||||
|
@ -81,6 +85,8 @@ def resize_argparse(args):
|
||||||
)
|
)
|
||||||
|
|
||||||
def main(argv):
|
def main(argv):
|
||||||
|
argv = vlogging.main_level_by_argv(argv)
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description=__doc__)
|
parser = argparse.ArgumentParser(description=__doc__)
|
||||||
|
|
||||||
parser.add_argument('pattern')
|
parser.add_argument('pattern')
|
||||||
|
|
Loading…
Reference in a new issue