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 argparse
import io import io
import os import os
import PIL.ExifTags
import PIL.Image import PIL.Image
import PIL.ImageFile import PIL.ImageFile
import string import string
import sys import sys
from voussoirkit import bytestring from voussoirkit import bytestring
from voussoirkit import imagetools
PIL.ImageFile.LOAD_TRUNCATED_IMAGES = True PIL.ImageFile.LOAD_TRUNCATED_IMAGES = True
for (ORIENTATION_KEY, val) in PIL.ExifTags.TAGS.items():
if val == 'Orientation':
break
def rejpg_argparse(args): def rejpg_argparse(args):
if args.recurse: if args.recurse:
from voussoirkit import spinal from voussoirkit import spinal
@ -37,19 +33,8 @@ def rejpg_argparse(args):
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) i = PIL.Image.open(filename)
exif = i._getexif()
# Preserve orientation according to exif i = imagetools.rotate_by_exif(i)
# 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.save(bytesio, format='jpeg', quality=80) i.save(bytesio, format='jpeg', quality=80)