Update function rotate_by_exif.

This commit is contained in:
voussoir 2025-03-24 22:16:39 -07:00
parent fa619073f8
commit 568667f35c

View file

@ -12,10 +12,8 @@ from voussoirkit import pathclass
_exifread = exifread _exifread = exifread
ORIENTATION_KEY = None ORIENTATION_KEY = {val: key for (key, val) in PIL.ExifTags.TAGS.items()}.get('Orientation', None)
for (ORIENTATION_KEY, val) in PIL.ExifTags.TAGS.items(): GEOLOCATION_KEY = {ifd.name: ifd.value for ifd in PIL.ExifTags.IFD}.get('GPSInfo', None)
if val == 'Orientation':
break
def checkerboard_image(color_1, color_2, image_size, checker_size) -> PIL.Image: def checkerboard_image(color_1, color_2, image_size, checker_size) -> PIL.Image:
''' '''
@ -166,7 +164,7 @@ def replace_color(image, from_color, to_color):
pixels[x, y] = to_color pixels[x, y] = to_color
return image return image
def rotate_by_exif(image): def rotate_by_exif(image, exif=None):
''' '''
Rotate the image according to its exif data, so that it will display Rotate the image according to its exif data, so that it will display
correctly even if saved without the exif. correctly even if saved without the exif.
@ -176,16 +174,18 @@ def rotate_by_exif(image):
You should be able to call image.save('filename.jpg', exif=exif) with You should be able to call image.save('filename.jpg', exif=exif) with
these returned values. these returned values.
(To my knowledge, I can not put the exif back into the Image object itself.
There is getexif but no setexif or putexif, etc.) To my knowledge, I can not put the exif back into the Image object itself.
There is getexif but no setexif or putexif, etc.
''' '''
# Thank you Scabbiaza # Thank you Scabbiaza
# https://stackoverflow.com/a/26928142 # https://stackoverflow.com/a/26928142
try: if exif is None:
exif = image.getexif() try:
except AttributeError: exif = image.getexif()
return (image, exif) except AttributeError:
return (image, exif)
if exif is None: if exif is None:
return (image, exif) return (image, exif)