1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| import exifread import requests
class PhotoExifInfo(): def __init__(self,photo_path): self.photo_path = photo_path self.baidu_map_ak = "your baidu map api key" self.image_info_dict={} self.tags ={} self.interested_keys = [ 'EXIF ExposureMode',\ 'EXIF ExposureTime',\ 'EXIF Flash',\ 'EXIF ISOSpeedRatings',\ 'Image Model',\ 'EXIF ExifImageWidth',\ 'EXIF ExifImageLength',\ 'Image DateTime',\ 'EXIF DateTimeOriginal',\ 'Image Make',\
] def get_tags(self): """ 获取照片信息 """ image_content = open(self.photo_path, 'rb') tags = exifread.process_file(image_content) self.tags = tags for item in self.interested_keys: try: info = tags[item] self.image_info_dict[item] = info except: print(f'{self.photo_path} has no attribute of {item}') continue image_content.close()
def get_lng_lat(self): """经纬度转换""" tags = self.tags try: LatRef = tags["GPS GPSLatitudeRef"].printable Lat = tags["GPS GPSLatitude"].printable[1:-1].replace(" ", "").replace("/", ",").split(",") Lat = float(Lat[0]) + float(Lat[1]) / 60 + float(Lat[2]) / 3600 if LatRef != "N": Lat = Lat * (-1) LonRef = tags["GPS GPSLongitudeRef"].printable Lon = tags["GPS GPSLongitude"].printable[1:-1].replace(" ", "").replace("/", ",").split(",") Lon = float(Lon[0]) + float(Lon[1]) / 60 + float(Lon[2]) / 3600 if LonRef != "E": Lon = Lon * (-1) return Lat,Lon except: print('Unable to get')
def get_city_info(self): result = self.get_lng_lat() if result: Lat, Lon = result url = "https://api.map.baidu.com/reverse_geocoding/v3/?ak="+self.baidu_map_ak+"&output=json&coordtype=wgs84ll&location=" + str(Lat) + ',' + str(Lon) response = requests.get(url).json() status = response['status'] if status == 0: address = response['result']['formatted_address'] if address != "": self.image_info_dict['Position'] = address else: print('baidu_map error') def get_image_info(self): self.get_tags() self.get_city_info() return self.image_info_dict if __name__ == '__main__': result = PhotoExifInfo("test.jpeg").get_image_info() for j, k in result.items(): print(f"{j} : {k}")
|