Get EXIF info from a picture file

import piexif

def _convert_to_degress(value):
    """Helper function to convert the GPS coordinates stored in the EXIF to degress in float format"""
    d0 = value[0][0]
    d1 = value[0][1]
    d = float(d0) / float(d1)

    m0 = value[1][0]
    m1 = value[1][1]
    m = float(m0) / float(m1)

    s0 = value[2][0]
    s1 = value[2][1]
    s = float(s0) / float(s1)

    return d + (m / 60.0) + (s / 3600.0)

if __name__ == '__main__':
    # taken by iPhone
    picturefile = '/Volumes/Mac Photos/MacPhotos.libraries/Photos.photoslibrary/Masters/2017/10/10/20171010-211712/IMG_6452.JPG'
    # taken by Huawei honor
    #picturefile = '/Volumes/Mac Photos/MacPhotos.libraries/Photos.photoslibrary/Masters/2017/10/14/20171014-120320/IMG_20161002_122011.jpg'
    exif = piexif.load(picturefile)
    for ifd in ("0th", "Exif", "GPS"):
        for tag in exif[ifd]:
            print(piexif.TAGS[ifd][tag]["name"], exif[ifd][tag])

    print("********************************")
    lat = exif["GPS"][piexif.GPSIFD.GPSLatitude]
    lng = exif["GPS"][piexif.GPSIFD.GPSLongitude]

    gpslat = _convert_to_degress(lat)
    gpslng = _convert_to_degress(lng)

    print(gpslat)
    print(gpslng)

GPS latitude and longitude are stored in tuple (array) as follows.

GPSLatitudeRef b'N'
GPSLatitude ((40, 1), (45, 1), (29082222, 1000000))
GPSLongitudeRef b'W'
GPSLongitude ((73, 1), (58, 1), (37654346, 1000000))
GPSAltitudeRef 0
GPSAltitude (3206, 100)
GPSTimeStamp ((16, 1), (20, 1), (10, 1))
GPSDateStamp b'2016:10:02'

你可能感兴趣的:(Get EXIF info from a picture file)