Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get GPSInfo and DateTimeOriginal from .getexif()? #5863

Closed
Olegt0rr opened this issue Dec 2, 2021 · 6 comments
Closed

How to get GPSInfo and DateTimeOriginal from .getexif()? #5863

Olegt0rr opened this issue Dec 2, 2021 · 6 comments
Labels

Comments

@Olegt0rr
Copy link

Olegt0rr commented Dec 2, 2021

Bug from #1477 still actual for public getexif method.

What did you do?

Trying to get valid GPSInfo data from EXIF:

from PIL import Image
from PIL.Image import Exif
from PIL.ExifTags import TAGS, GPSTAGS

def get_exif(file_name) -> Exif:
    image: Image.Image = Image.open(file_name)
    return image.getexif()


def get_labeled_exif(exif):
    return {
        TAGS.get(key, key): value
        for key, value in exif.items()
    }


def get_geo(labeled_exif):
    gps_info = labeled_exif.get("GPSInfo", {})
    return {
        GPSTAGS.get(key, key): value
        for key, value in gps_info.items()
    }

if __name__ == '__main__':
    exif = get_exif("Tests/images/exif_gps.jpg")
    labeled_exif = get_labeled_exif(exif)
    geo = get_geo(labeled_exif)
    print(geo)

What did you expect to happen?

Result

{'GPSVersionID': b'\x00\x00\x00\x01', 'GPSLatitude': 4294967295.0, 'GPSAltitudeRef': b'\x01', 'GPSDateStamp': '1999:99:99 99:99:99', 'GPSDifferential': 65535}

What actually happened?

Traceback (most recent call last):
  line 33, in <module>
    geo = get_geo(labeled_exif)
  line 22, in get_geo
    for key, value in gps_info.items()
AttributeError: 'int' object has no attribute 'items'

What are your OS, Python and Pillow versions?

  • OS: macOS
  • Python: 3.8
  • Pillow: 8.4.0

Workaround

Use image._getexif() instead of image.getexif()

Tests

So why the Pillow tests pass successfully?
That's why:

with Image.open("Tests/images/exif_gps.jpg") as im:
exif = im._getexif()
assert exif[gps_index] == expected_exif_gps

@radarhere radarhere added the Exif label Dec 2, 2021
@radarhere
Copy link
Member

Hi. My explanation is that GPSInfo is an IFD - https://www.awaresystems.be/imaging/tiff/tifftags/gpsifd.html.

The int that you're seeing is not the data itself, but the offset to the data, in case anyone would like to access that.

Instead, you can use get_ifd to retrieve the GPS data.

See what you think of this.

from PIL import Image
from PIL.Image import Exif
from PIL.ExifTags import TAGS, GPSTAGS

def get_exif(file_name) -> Exif:
    image: Image.Image = Image.open(file_name)
    return image.getexif()


def get_geo(exif):
    for key, value in TAGS.items():
        if value == "GPSInfo":
            break
    gps_info = exif.get_ifd(key)
    return {
        GPSTAGS.get(key, key): value
        for key, value in gps_info.items()
    }

if __name__ == '__main__':
    exif = get_exif("Tests/images/exif_gps.jpg")
    geo = get_geo(exif)
    print(geo)

@radarhere
Copy link
Member

You gave my response a thumbs up. Does that mean you're happy with this and the issue can be closed?

@Olegt0rr
Copy link
Author

Olegt0rr commented Dec 4, 2021

@radarhere, I still can't understand behaviour of public exif method, e.g.:
DateTimeOriginal exists in ._get_exif() and absent in .get_exif() result.
Also can't get it by .get_ifd()

@radarhere
Copy link
Member

DateTimeOriginal is within the EXIF IFD - https://www.awaresystems.be/imaging/tiff/tifftags/privateifd/exif.html

_getexif() merged the EXIF IFD into the rest of the top level items. This makes it harder to figure out afterwards what was in the EXIF IFD and what was at the top level to begin with. So since #4947, getexif() no longer does that. Instead, DateTimeOriginal can be found like so -

from PIL import Image
from PIL.Image import Exif
from PIL.ExifTags import TAGS

def get_exif(file_name) -> Exif:
    image: Image.Image = Image.open(file_name)
    return image.getexif()


def get_exif_ifd(exif):
    for key, value in TAGS.items():
        if value == "ExifOffset":
            break
    info = exif.get_ifd(key)
    return {
        TAGS.get(key, key): value
        for key, value in info.items()
    }

if __name__ == '__main__':
    exif = get_exif("Tests/images/exif_gps.jpg")
    print(get_exif_ifd(exif))

gives

{'DateTimeOriginal': '2099:09:29 10:10:10', 'ExposureBiasValue': -0.9999999995343387, 'OECF': b'\xaa\xaa\xaa\xaa\xaa\xaa', 'Sharpness': 65535, 'LensSpecification': (1.0, 1.0, 1.0, 1.0), 'ISOSpeed': 4294967295, 'LensMake': 'LensMake', 'ExposureTime': 4294967295.0}

@Olegt0rr
Copy link
Author

Olegt0rr commented Dec 4, 2021

Thanks! Now that's clear for me :)

I'll rename the issue to help others find the explanation

@Olegt0rr Olegt0rr closed this as completed Dec 4, 2021
@Olegt0rr Olegt0rr changed the title Wrong GPSInfo from .getexif() How to get GPSInfo and DateTimeOriginal from .getexif()? Dec 4, 2021
@jessielw
Copy link

DateTimeOriginal is within the EXIF IFD - https://www.awaresystems.be/imaging/tiff/tifftags/privateifd/exif.html

_getexif() merged the EXIF IFD into the rest of the top level items. This makes it harder to figure out afterwards what was in the EXIF IFD and what was at the top level to begin with. So since #4947, getexif() no longer does that. Instead, DateTimeOriginal can be found like so -

from PIL import Image
from PIL.Image import Exif
from PIL.ExifTags import TAGS

def get_exif(file_name) -> Exif:
    image: Image.Image = Image.open(file_name)
    return image.getexif()


def get_exif_ifd(exif):
    for key, value in TAGS.items():
        if value == "ExifOffset":
            break
    info = exif.get_ifd(key)
    return {
        TAGS.get(key, key): value
        for key, value in info.items()
    }

if __name__ == '__main__':
    exif = get_exif("Tests/images/exif_gps.jpg")
    print(get_exif_ifd(exif))

gives

{'DateTimeOriginal': '2099:09:29 10:10:10', 'ExposureBiasValue': -0.9999999995343387, 'OECF': b'\xaa\xaa\xaa\xaa\xaa\xaa', 'Sharpness': 65535, 'LensSpecification': (1.0, 1.0, 1.0, 1.0), 'ISOSpeed': 4294967295, 'LensMake': 'LensMake', 'ExposureTime': 4294967295.0}

I couldn't find this anywhere in the documentation. This works really well, thanks for sharing here.

EddyXorb added a commit to EddyXorb/mow that referenced this issue Dec 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants