一。引言 先看获取到的效果拍摄时间:2021:12:1816:22:13照片拍摄地址:(内蒙古自治区包头市昆都仑区,内蒙古自治区,包头市,昆都仑区,多米幼儿园东南360米) 我们的女朋友给我们发来一张照片我们如何获取到她的位置呢? 用手机拍照会带着GPS信息,原来没注意过这个,因此查看下并使用代码获取照片里的GPS信息 查看图片文件属性 1。读取照片信息,获取坐标 ExifRead PythonlibrarytoextractEXIFdatafromtiffandjpegfiles。 安装pipinstallexifread 读取GPSimportexifreadimportredefread():GPS{}datefopen(C:Users24190Desktop小朱学长。jpg,rb)contentsexifread。processfile(f)forkeyincontents:ifkeyGPSGPSLongitude:print(经度,contents〔key〕,contents〔GPSGPSLatitudeRef〕)elifkeyGPSGPSLatitude:print(纬度,contents〔key〕,contents〔GPSGPSLongitudeRef〕)print(contents)read() 运行 我们得到了一个简易的gps地址 如果想要读取全部的拍摄信息:读取照片的GPS经纬度信息deffindGPSimage(picpath):GPS{}datewithopen(picpath,rb)asf:tagsexifread。processfile(f)fortag,valueintags。items():纬度ifre。match(GPSGPSLatitudeRef,tag):GPS〔GPSLatitudeRef〕str(value)经度elifre。match(GPSGPSLongitudeRef,tag):GPS〔GPSLongitudeRef〕str(value)海拔elifre。match(GPSGPSAltitudeRef,tag):GPS〔GPSAltitudeRef〕str(value)elifre。match(GPSGPSLatitude,tag):try:matchresultre。match(〔(w),(w),(w。)(w。)〕,str(value))。groups()GPS〔GPSLatitude〕int(matchresult〔0〕),int(matchresult〔1〕),int(matchresult〔2〕)except:deg,min,sec〔x。replace(,)forxinstr(value)〔1:1〕。split(,)〕GPS〔GPSLatitude〕latitudeandlongitudeconverttodecimalsystem(deg,min,sec)elifre。match(GPSGPSLongitude,tag):try:matchresultre。match(〔(w),(w),(w。)(w。)〕,str(value))。groups()GPS〔GPSLongitude〕int(matchresult〔0〕),int(matchresult〔1〕),int(matchresult〔2〕)except:deg,min,sec〔x。replace(,)forxinstr(value)〔1:1〕。split(,)〕GPS〔GPSLongitude〕latitudeandlongitudeconverttodecimalsystem(deg,min,sec)elifre。match(GPSGPSAltitude,tag):GPS〔GPSAltitude〕str(value)elifre。match(。Date。,tag):datestr(value)return{GPSinformation:GPS,dateinformation:date}2。通过baiduMap的API将GPS信息转换成地址。 众所周知gps和百度的经纬度会有误差,那么我们需要调用百度转换接口,这个百度目前没有开源。通过baiduMap的API将GPS信息转换成地址。deffindaddressfromGPS(GPS):使用GeocodingAPI把经纬度坐标转换为结构化地址。:paramGPS::return:secretkeyzbLsuDDL4CS2U0M4KezOZZbGUY9iWtVfifnotGPS〔GPSinformation〕:return该照片无GPS信息lat,lngGPS〔GPSinformation〕〔GPSLatitude〕,GPS〔GPSinformation〕〔GPSLongitude〕baidumapapihttp:api。map。baidu。comgeocoderv2?ak{0}callbackrenderReverselocation{1},{2}soutputjsonpois0。format(secretkey,lat,lng)responserequests。get(baidumapapi)contentresponse。text。replace(renderReverserenderReverse(,)〔:1〕print(content)baidumapaddressjson。loads(content)formattedaddressbaidumapaddress〔result〕〔formattedaddress〕provincebaidumapaddress〔result〕〔addressComponent〕〔province〕citybaidumapaddress〔result〕〔addressComponent〕〔city〕districtbaidumapaddress〔result〕〔addressComponent〕〔district〕locationbaidumapaddress〔result〕〔sematicdescription〕returnformattedaddress,province,city,district,location 然后在主函数输出: 二。源码附上!!!codingutf8作者:小朱学长微信公众号:八方支猿importexifreadimportreimportjsonimportrequestsimportos转换经纬度格式deflatitudeandlongitudeconverttodecimalsystem(arg):经纬度转为小数,paramarg::return:十进制小数returnfloat(arg〔0〕)((float(arg〔1〕)(float(arg〔2〕。split()〔0〕)float(arg〔2〕。split()〔1〕)60))60)读取照片的GPS经纬度信息deffindGPSimage(picpath):GPS{}datewithopen(picpath,rb)asf:tagsexifread。processfile(f)fortag,valueintags。items():纬度ifre。match(GPSGPSLatitudeRef,tag):GPS〔GPSLatitudeRef〕str(value)经度elifre。match(GPSGPSLongitudeRef,tag):GPS〔GPSLongitudeRef〕str(value)海拔elifre。match(GPSGPSAltitudeRef,tag):GPS〔GPSAltitudeRef〕str(value)elifre。match(GPSGPSLatitude,tag):try:matchresultre。match(〔(w),(w),(w。)(w。)〕,str(value))。groups()GPS〔GPSLatitude〕int(matchresult〔0〕),int(matchresult〔1〕),int(matchresult〔2〕)except:deg,min,sec〔x。replace(,)forxinstr(value)〔1:1〕。split(,)〕GPS〔GPSLatitude〕latitudeandlongitudeconverttodecimalsystem(deg,min,sec)elifre。match(GPSGPSLongitude,tag):try:matchresultre。match(〔(w),(w),(w。)(w。)〕,str(value))。groups()GPS〔GPSLongitude〕int(matchresult〔0〕),int(matchresult〔1〕),int(matchresult〔2〕)except:deg,min,sec〔x。replace(,)forxinstr(value)〔1:1〕。split(,)〕GPS〔GPSLongitude〕latitudeandlongitudeconverttodecimalsystem(deg,min,sec)elifre。match(GPSGPSAltitude,tag):GPS〔GPSAltitude〕str(value)elifre。match(。Date。,tag):datestr(value)return{GPSinformation:GPS,dateinformation:date}通过baiduMap的API将GPS信息转换成地址。deffindaddressfromGPS(GPS):使用GeocodingAPI把经纬度坐标转换为结构化地址。:paramGPS::return:secretkeyzbLsuDDL4CS2U0M4KezOZZbGUY9iWtVfifnotGPS〔GPSinformation〕:return该照片无GPS信息lat,lngGPS〔GPSinformation〕〔GPSLatitude〕,GPS〔GPSinformation〕〔GPSLongitude〕baidumapapihttp:api。map。baidu。comgeocoderv2?ak{0}callbackrenderReverselocation{1},{2}soutputjsonpois0。format(secretkey,lat,lng)responserequests。get(baidumapapi)contentresponse。text。replace(renderReverserenderReverse(,)〔:1〕print(content)baidumapaddressjson。loads(content)formattedaddressbaidumapaddress〔result〕〔formattedaddress〕provincebaidumapaddress〔result〕〔addressComponent〕〔province〕citybaidumapaddress〔result〕〔addressComponent〕〔city〕districtbaidumapaddress〔result〕〔addressComponent〕〔district〕locationbaidumapaddress〔result〕〔sematicdescription〕returnformattedaddress,province,city,district,locationifnamemain:GPSinfofindGPSimage(picpath小朱学长。jpg)addressfindaddressfromGPS(GPSGPSinfo)print(拍摄时间:GPSinfo。get(dateinformation))print(照片拍摄地址:str(address))注意事项 1。照片的地址信息等,一般的手机相机默认是打开的。 2。微信和QQ里面发送原图,信息都会完整的保留下来。 3。代码里面需要处理在照片我放到了代码的同文件夹下,所以没有写路径,大家可以自己写路径,或者放到与代码相同的路径下即可。 (有任何问题的话,欢迎评论区或者私信我哦,我都会看的!) :heart::heart::heart:如果你喜欢的话,就不要吝惜你的一键三连了