2021-06-08 01:46:38 +00:00
|
|
|
import argparse
|
|
|
|
import PIL.Image
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from voussoirkit import imagetools
|
2021-09-24 06:42:34 +00:00
|
|
|
from voussoirkit import pathclass
|
2021-06-08 01:46:38 +00:00
|
|
|
from voussoirkit import pipeable
|
|
|
|
from voussoirkit import vlogging
|
|
|
|
|
|
|
|
log = vlogging.getLogger(__name__, 'rotate')
|
|
|
|
|
|
|
|
def rotate_argparse(args):
|
|
|
|
if args.angle is None and not args.exif:
|
2021-09-24 06:42:34 +00:00
|
|
|
log.fatal('Either an angle or --exif must be provided.')
|
2021-06-08 01:46:38 +00:00
|
|
|
return 1
|
|
|
|
|
2021-09-24 06:42:34 +00:00
|
|
|
patterns = pipeable.input(args.pattern, skip_blank=True, strip=True)
|
2021-11-08 19:38:10 +00:00
|
|
|
files = pathclass.glob_many_files(patterns)
|
2021-09-24 06:42:34 +00:00
|
|
|
|
|
|
|
for file in files:
|
|
|
|
image = PIL.Image.open(file.absolute_path)
|
2023-03-19 18:21:33 +00:00
|
|
|
icc_profile = image.info.get('icc_profile')
|
2021-06-08 01:46:38 +00:00
|
|
|
|
|
|
|
if args.exif:
|
|
|
|
(new_image, exif) = imagetools.rotate_by_exif(image)
|
|
|
|
if new_image is image:
|
2021-09-24 06:42:34 +00:00
|
|
|
log.debug('%s doesn\'t need exif rotation.', file.absolute_path)
|
2021-06-08 01:46:38 +00:00
|
|
|
continue
|
|
|
|
image = new_image
|
|
|
|
else:
|
|
|
|
exif = image.getexif()
|
|
|
|
image = image.rotate(args.angle, expand=True)
|
|
|
|
|
|
|
|
if args.inplace:
|
2021-09-24 06:42:34 +00:00
|
|
|
newname = file
|
2021-06-08 01:46:38 +00:00
|
|
|
else:
|
|
|
|
if args.exif:
|
|
|
|
suffix = f'_exifrot'
|
|
|
|
else:
|
|
|
|
suffix = f'_{args.angle}'
|
2021-09-24 06:42:34 +00:00
|
|
|
|
|
|
|
base = file.replace_extension('').basename
|
|
|
|
newname = base + suffix
|
|
|
|
newname = file.parent.with_child(newname).add_extension(file.extension)
|
|
|
|
|
2021-10-25 03:54:51 +00:00
|
|
|
pipeable.stdout(newname.absolute_path)
|
2023-03-19 18:21:33 +00:00
|
|
|
image.save(newname.absolute_path, exif=exif, quality=args.quality, icc_profile=icc_profile)
|
2021-06-08 01:46:38 +00:00
|
|
|
|
2021-06-22 05:11:19 +00:00
|
|
|
@vlogging.main_decorator
|
2021-06-08 01:46:38 +00:00
|
|
|
def main(argv):
|
|
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
|
|
|
|
|
|
parser.add_argument('pattern')
|
|
|
|
parser.add_argument('angle', nargs='?', type=int, default=None)
|
|
|
|
parser.add_argument('--exif', action='store_true')
|
|
|
|
parser.add_argument('--inplace', action='store_true')
|
|
|
|
parser.add_argument('--quality', type=int, default=100)
|
|
|
|
parser.set_defaults(func=rotate_argparse)
|
|
|
|
|
|
|
|
args = parser.parse_args(argv)
|
|
|
|
return args.func(args)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
raise SystemExit(main(sys.argv[1:]))
|