0

I'm using exifread to get some Exif info from photos.

Currently, I'm trying to get the GPS Latitude and Longitude, which I can do:

def main():
    images = image_paths(IMAGE_FOLDER) # folder is say "Sample Photos\\"
    info = {}
    for img in images:
        _lat, _lon = "", ""
        tags = exifread.process_file(open(img,'rb'))       

        for i in tags.keys():
            if i == "GPS GPSLatitude":
                print(i, ":::", tags[i])

This prints what I expect, they key name and the values:

GPS GPSLatitude ::: [32, 52, 66443/1250]
GPS GPSLatitude ::: [32, 52, 531699/10000]
GPS GPSLatitude ::: [32, 52, 531699/10000]
GPS GPSLatitude ::: [32, 52, 132789/2500]
GPS GPSLatitude ::: [32, 52, 265817/5000]

But, to skip doing that loop and just get the key/value pairs quickly, I'm trying instead (this replaces the for i ... loop)

_lon = tags["GPS GPSLatitude"]

But I get an error:

KeyError: 'GPS GPSLatitude'

How do I access "GPS GPSLatitude" (and "GPS GPSLongitude") without looping through the keys of tags?

Edit:

tags is <class 'dict'>

BruceWayne
  • 22,923
  • 15
  • 65
  • 110
  • Not familiar with `exifread`, what is the `type(tags)`? – pstatix May 10 '18 at 20:00
  • 1
    Are you sure _every_ image has the tag? Your output would be compatible with some having it, some not. – Paul Panzer May 10 '18 at 20:02
  • 1
    @hiroprotagonist: That space appears to be coming from `print`, which automatically prints a space between arguments unless told to do otherwise. – user2357112 May 10 '18 at 20:03
  • 1
    You only print when that specific image has that tag, but you are looping over multiple images. **Not all images have that tag**, and when they don't you are not printing. – Martijn Pieters May 10 '18 at 20:04
  • @MartijnPieters - Darn it, that's it exactly. I added a `Try/Except` and that fixed it. Sorry for not catching that earlier :( – BruceWayne May 10 '18 at 20:05
  • 1
    @RuudHelderman - Wow, how didn't that post show up when I searched SO?? That's exactly it. – BruceWayne May 10 '18 at 20:07

2 Answers2

3

Is it possible that some of the tags do not have the key 'GPS GPSLatitude'?

Maybe you can try

_lat = tags.get('GPS GPSLatitude', '')

This way, _lat will be set to the latitude value if it exists, otherwise it will be set to an empty string.

  • 1
    Yeah, that's it. Most *should* have the GPS, which is why I didn't think to check, but apparently not all do. Thanks! (Also thanks for teaching me the `get()` function, pretty neat). – BruceWayne May 10 '18 at 20:05
0

If any image doesn't have that tag, you need to handle that exception to prevent the crash. For instance:

def main():
    images = image_paths(IMAGE_FOLDER) # folder is say "Sample Photos\\"
    info = {}
    for img in images:
        _lat, _lon = "", ""
        tags = exifread.process_file(open(img,'rb'))       

        try:
            _lon = tags["GPS GPSLongitude"]
            # _lat = tags["GPS GPSLatitude"]  # presumably you also want this
            # do something with these... perhaps add to info
        except KeyError:
            pass  # move on
Arthur Dent
  • 1,828
  • 13
  • 21
  • you have a typo, `_lon` should not be equal to the Latitude variable and its pointless to initiate both variables as empty in each loop if you're just going to overwrite them – eagle May 10 '18 at 20:08
  • Thanks, @eagle --> was just intended to be an example but good catch on the typo – Arthur Dent May 11 '18 at 02:15