Move rotate_by_exif to voussoirkit/imagetools.

This commit is contained in:
voussoir 2021-01-05 20:29:54 -08:00
parent 96a4520c9c
commit 0c78d7f96c

View file

@ -4,20 +4,16 @@ Recompress all jpg images in the current directory.
import argparse
import io
import os
import PIL.ExifTags
import PIL.Image
import PIL.ImageFile
import string
import sys
from voussoirkit import bytestring
from voussoirkit import imagetools
PIL.ImageFile.LOAD_TRUNCATED_IMAGES = True
for (ORIENTATION_KEY, val) in PIL.ExifTags.TAGS.items():
if val == 'Orientation':
break
def rejpg_argparse(args):
if args.recurse:
from voussoirkit import spinal
@ -37,19 +33,8 @@ def rejpg_argparse(args):
print(''.join(c for c in filename if c in string.printable))
bytesio = io.BytesIO()
i = PIL.Image.open(filename)
exif = i._getexif()
# Preserve orientation according to exif
# Thank you Scabbiaza
# https://stackoverflow.com/a/26928142
if exif is None:
pass
elif exif[ORIENTATION_KEY] == 3:
i = i.rotate(180, expand=True)
elif exif[ORIENTATION_KEY] == 6:
i = i.rotate(270, expand=True)
elif exif[ORIENTATION_KEY] == 8:
i = i.rotate(90, expand=True)
i = imagetools.rotate_by_exif(i)
i.save(bytesio, format='jpeg', quality=80)