Use getexif instead of info.get('exif'). Add --quality where missing.

This commit is contained in:
voussoir 2021-11-09 12:08:38 -08:00
parent 357669a684
commit 15141d22cb
No known key found for this signature in database
GPG key ID: 5F7554F8C26DACCB
3 changed files with 10 additions and 6 deletions

View file

@ -5,7 +5,7 @@ import sys
from voussoirkit import pathclass
from voussoirkit import pipeable
def crop(file, crops, *, inplace=False):
def crop(file, crops, *, inplace=False, quality=100):
image = PIL.Image.open(file.absolute_path)
if len(crops) == 2:
crops.extend(image.size)
@ -27,7 +27,7 @@ def crop(file, crops, *, inplace=False):
newname = file.parent.with_child(base + suffix).add_extension(file.extension)
pipeable.stdout(newname.absolute_path)
image.save(newname.absolute_path, exif=image.info.get('exif', b''), quality=100)
image.save(newname.absolute_path, exif=image.getexif(), quality=quality)
def crop_argparse(args):
patterns = pipeable.input(args.pattern, skip_blank=True, strip=True)
@ -38,6 +38,7 @@ def crop_argparse(args):
file,
crops=args.crops,
inplace=args.inplace,
quality=args.quality,
)
return 0
@ -47,6 +48,7 @@ def main(argv):
parser.add_argument('pattern')
parser.add_argument('crops', nargs='+', type=int, default=None)
parser.add_argument('--inplace', action='store_true')
parser.add_argument('--quality', type=int, default=100)
parser.set_defaults(func=crop_argparse)
args = parser.parse_args(argv)

View file

@ -2,10 +2,11 @@ import argparse
import PIL.Image
import sys
from voussoirkit import imagetools
from voussoirkit import pathclass
from voussoirkit import pipeable
def grayscale(filename, *, inplace=False):
def grayscale(filename, *, inplace=False, quality=100):
filename = pathclass.Path(filename)
basename = filename.replace_extension('').basename
@ -20,14 +21,14 @@ def grayscale(filename, *, inplace=False):
image = PIL.Image.open(filename.absolute_path)
image = image.convert('LA').convert(image.mode)
image.save(new_filename.absolute_path, exif=image.info.get('exif', b''))
image.save(new_filename.absolute_path, exif=image.getexif(), quality=quality)
return new_filename
def grayscale_argparse(args):
patterns = pipeable.input_many(args.patterns, skip_blank=True, strip=True)
files = pathclass.glob_many_files(patterns)
for file in files:
new_filename = grayscale(file, inplace=args.inplace)
new_filename = grayscale(file, inplace=args.inplace, quality=args.quality)
if new_filename:
pipeable.stdout(new_filename.absolute_path)
@ -38,6 +39,7 @@ def main(argv):
parser.add_argument('patterns', nargs='+')
parser.add_argument('--inplace', action='store_true')
parser.add_argument('--quality', type=int, default=100)
parser.set_defaults(func=grayscale_argparse)
args = parser.parse_args(argv)

View file

@ -101,7 +101,7 @@ def resize(
image = image.convert('RGB')
pipeable.stdout(new_name.absolute_path)
image.save(new_name.absolute_path, exif=image.info.get('exif', b''), quality=quality)
image.save(new_name.absolute_path, exif=image.getexif(), quality=quality)
def resize_argparse(args):
patterns = pipeable.input(args.pattern, skip_blank=True, strip=True)