""" 模块:python3 getImgLonLatOfLocation_exifread_geopy.py 功能:python3 读取照片的经纬度、所属地区。 参考: https://www.cnblogs.com/Qqun821460695/p/12054674.html https://www.biaodianfu.com/exif-python.html 安装库: pip3 install exifread pip3 install geopy 知识点: 1.可交换图像文件格式(英语:Exchangeable image file format,官方简称Exif), 是专门为数码相机的照片设定的,可以记录数码照片的属性信息和拍摄数据。 2.exifread.process_file(f, stop_tag='UNDEF', details=True, strict=False, debug=False) 处理一个图片文件 (期望一个打开的文件对象 )。 这是必须处理exif标准的所有任意讨厌位的函数。 3.imgDetail 类似: { 'Image Make': (0x010F) ASCII=Point Grey Research @ 86, 'Image Model': (0x0110) ASCII=Ladybug @ 106, 'Image Software': (0x0131) ASCII=Ladybug SDK @ 114, 'GPS GPSVersionID': (0x0000) Byte=[2, 2, 0, 0] @ 150, 'GPS GPSLatitudeRef': (0x0001) ASCII=N @ 162, 'GPS GPSLatitude': (0x0002) Ratio=[39(度), 1(分), 5984939/100000(秒)] @ 262, 'GPS GPSLongitude': (0x0004) Ratio=[117(度), 12(分), 3505979/100000(秒)] @ 286, 'GPS GPSAltitudeRef': (0x0005) Byte=0 @ 198, 'GPS GPSAltitude': (0x0006) Ratio=5/2 @ 310, 'GPS GPSSpeedRef': (0x000C) ASCII=K @ 222, 'GPS GPSSpeed': (0x000D) Ratio=263/50 @ 318, 'GPS GPSTimeStamp': (0x0007) Ratio=[3(时), 2(分), 54(秒)] @ 326, 'GPS GPSDate': (0x001D) ASCII=0015(年):02(月):12(日) @ 350, 'Image GPSInfo': (0x8825) Long=140 @ 54, 'Image ExifOffset': (0x8769) Long=126 @ 66, 'Image Tag 0x0003': (0x0003) ASCII=E @ 78, 'EXIF ExifVersion': (0x9000) Undefined=0220 @ 136 } """ import os import exifread from geopy.geocoders import Nominatim def getImgLonLat(imgFilepath): """ 获取照片的经纬度。 :param imgFilepath: 照片文件路径。 :return: 照片的经纬度。 """ print(os.path.basename(imgFilepath)) with open(imgFilepath, 'rb') as imgFile: imgDetail = exifread.process_file(imgFile) # 1.照片拍摄时间 GPSTimeStamp = imgDetail["GPS GPSTimeStamp"].printable # print("GPSTimeStamp:", GPSTimeStamp) # GPSTimeStamp: [3, 2, 54] GPSDate = imgDetail["GPS GPSDate"].printable # print("GPSDate:", GPSDate) # GPSDate: 0015:02:12 imgCreateTime = GPSDate + " " + GPSTimeStamp[1:-1].replace(", ", ":") print("imgCreateTime:", imgCreateTime) # imgCreateTime: 0015:02:12 3:2:54 # 2.照片的经度 img_longitude_ref = imgDetail["Image Tag 0x0003"].printable img_longitude = imgDetail["GPS GPSLongitude"].printable[1:-1].replace(" ", "").replace("/", ",").split(",") img_longitude = float(img_longitude[0]) + float(img_longitude[1]) / 60 + float(img_longitude[2]) / float( img_longitude[3]) / 3600 if img_longitude_ref != "E": img_longitude *= -1 # 3.照片的纬度 img_latitude_ref = imgDetail["GPS GPSLatitudeRef"].printable img_latitude = imgDetail["GPS GPSLatitude"].printable[1:-1].replace(" ", "").replace("/", ",").split(",") img_latitude = float(img_latitude[0]) + float(img_latitude[1]) / 60 + float(img_latitude[2]) / float( img_latitude[3]) / 3600 if img_latitude_ref != "N": img_latitude = img_latitude * -1 # 4.摄像头高度。 img_altitude_ref = imgDetail["GPS GPSAltitudeRef"].printable img_altitude = imgDetail["GPS GPSAltitude"].printable # print(img_altitude_ref, eval(img_altitude) - eval(img_altitude_ref)) print("imgCreatePoint:", img_longitude, img_latitude, eval(img_altitude) - eval(img_altitude_ref)) # imgCreatePoint: 117.20973883055557 39.03329149722222 2.5 return img_longitude, img_latitude def getImgLocation(lat, lon): """ 根据照片文件的经纬度,获取其所属地区的位置信息。 :param lat: 照片的纬度。 :param lon: 照片的精度。 :return: """ # queryPoint = str(lat) + ', ' + str(lon) queryPoint = (lat, lon) geolocator = Nominatim() # location,照片所在地区的(地图标注)位置。 location = geolocator.reverse(queryPoint) print("\nlocation:", location) # location: 西青区 (Xiqing), 天津市, 300350, China 中国 print("location.address:", location.address) # location.address: 西青区 (Xiqing), 天津市, 300350, China 中国 print("location.point:", location.point) # location.point: 39 1m 59.5574s N, 117 12m 35.005s E print(location.latitude, location.longitude, location.altitude) # 39.03321039356817 117.2097236153773 0.0 print(location.raw) #{'place_id': 129190182, 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright', # 'osm_type': 'way', 'osm_id': 185698530, # 'lat': '39.03321039356817', # 'lon': '117.2097236153773', # 'display_name': '西青区 (Xiqing), 天津市, 300350, China 中国', # 'address': {'county': '西青区 (Xiqing)', 'state': '天津市', # 'postcode': '300350', 'country': 'China 中国', 'country_code': 'cn'}, # 'boundingbox': ['39.0317814', '39.0351962', '117.1988215', '117.2236431'] # } if __name__ == '__main__': imgLonLat = getImgLonLat( r'F:\Gaoshengjie\LongGeTasks\streetView\output\waihuanlu\ladybug_panoramic_000091.jpg') # getImgLocation(imgLonLat[1], imgLonLat[0])