提取图像元数据的问题

时间:2017-07-04 07:59:20

标签: python python-imaging-library

我写了一个python代码来获取某个图像的元数据,但是我收到了这个错误:

  

NameError:全局名称' _get_if_exist'未定义

你知道如何解决这个问题。

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

def get_exif_data(image):
exif_data = {}
info = getattr(image, '_getexif', lambda: None)()
if info:
    for tag, value in info.items():
        decoded = TAGS.get(tag, tag)
        if decoded == "GPSInfo":
            gps_data = {}
            for t in value:
                sub_decoded = GPSTAGS.get(t, t)
                gps_data[sub_decoded] = value[t]

            exif_data[decoded] = gps_data
        else:
            exif_data[decoded] = value

return exif_data

return data.get(key, None)

 def _convert_to_degress(value):
  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)

def get_lat_lon(exif_data):

 lat = None
 lon = None

if "GPSInfo" in exif_data:      
    gps_info = exif_data["GPSInfo"]

    gps_latitude = _get_if_exist(gps_info, "GPSLatitude")
    gps_latitude_ref = _get_if_exist(gps_info, 'GPSLatitudeRef')
    gps_longitude = _get_if_exist(gps_info, 'GPSLongitude')
    gps_longitude_ref = _get_if_exist(gps_info, 'GPSLongitudeRef')

    if gps_latitude and gps_latitude_ref and gps_longitude and 
    gps_longitude_ref:
        lat = _convert_to_degress(gps_latitude)
        if gps_latitude_ref != "N":                     
            lat = 0 - lat

        lon = _convert_to_degress(gps_longitude)
        if gps_longitude_ref != "E":
            lon = 0 - lon

return lat, lon



if __name__ == "__main__":
image =   
Image.open("C:\\Users\\Mouqsit\\Desktop\\Bouznika_Panos_Complement\\*.jp
g") 
exif_data = get_exif_data(image)
print get_lat_lon(exif_data)

此代码用于从图像中提取gps元数据

1 个答案:

答案 0 :(得分:0)

_get_if_exist()助手默认情况下不存在,也不会导入。它的代码是:

def _get_if_exist(data, key):
    if key in data:
        return data[key]
    return None

原作是here