当前位置: 移动技术网 > IT编程>脚本编程>Python > python实现提取照片中的GPS坐标

python实现提取照片中的GPS坐标

2020年07月14日  | 移动技术网IT编程  | 我要评论

作者:小刚
一位苦于信息安全的萌新小白帽,记得关注给个赞,谢谢

python实现提取照片中的GPS坐标

图片中的GPS

通常我们拍的照片,除了我们所能看到的图像信息,图片中还存在着手机的信息,如果拍照时开启定位,照片中会存在GPS信息
此次通过python提取图片中的GPS,并将度分秒格式转换成十进制,通过地图查找出拍摄位置。

实现代码

#文件名PictureGps.py
import exifread
import re

#读取图片中的信息
def FindGPSTime(filePath):
    GPS={}
    Data=""
    f=open(filePath,'rb') #读取图片信息
    tags=exifread.process_file(f)


    for tag,value in tags.items():
        if re.match('GPS GPSLatitudeRef',tag):
            GPS['GPSLatitudeRef(纬度标识)']=str(value)
        elif re.match('GPS GPSLongitudeRef',tag):
            GPS['GPSLongitudeRef(经度标识)']=str(value)
        elif re.match('GPS GPSAltitudeRef',tag):
            GPS['GPSAltitudeRef(高度标识)']=str(value)
        elif re.match('GPS GPSLatitude',tag):
            try:
                match_result=re.match('\[(\w*), (\w*), (\w.*)/(\w.*)\]',str(value)).groups()   #匹配临近的字符
                GPS['GPSLatitude(纬度)']=[int(match_result[0]),int(match_result[1]),int(match_result[2])/int(match_result[3])]
            except:
                GPS['GPSLatitude(纬度)']=str(value)
        elif re.match('GPS GPSLongitude',tag):
            try:
                match_result=re.match('\[(\w*), (\w*), (\w.*)/(\w.*)\]',str(value)).groups()
                GPS['GPSLongitude(经度)']=[int(match_result[0]),int(match_result[1]),int(match_result[2])/int(match_result[3])]
            except:
                GPS['GPSLongitude(经度)']=str(value)
        elif re.match('GPS GPSAltitude',tag):
            GPS['GPSAltitude(高度)']=str(value)
        elif re.match('Image DateTime',tag):
            Data=str(value)
    return {'GPS 信息':GPS,'时间信息':Data}

#将经纬度的度分秒转换成十进制格式	
def GPSSwitch(filepath):
	
	print(FindGPSTime(filepath))
	PictureGPS = FindGPSTime(filepath)
	GPSLongitude = PictureGPS['GPS 信息']['GPSLongitude(经度)']#经度的列表内容
	GPSLatitude = PictureGPS['GPS 信息']['GPSLatitude(纬度)'] #纬度的列表内容
	x = GPSLongitude[0] + GPSLongitude[1] / 60 + GPSLongitude[2] / 3600 #进制转换
	y = GPSLatitude[0] + GPSLatitude[1] / 60 + GPSLatitude[2] / 3600
	return {'经度转换':x,'纬度转换':y}

if __name__=='__main__':
	print(GPSSwitch("1.jpg")) #需要提取信息的图片名

读取的信息

在这里插入图片描述
通过地图查找经纬度,GPS定位有偏差,相差不远

在这里插入图片描述

本文地址:https://blog.csdn.net/weixin_43221560/article/details/107316316

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网