女友独自旅游不放心?Python版通过照片定位GPS系统!超好用!

279 阅读3分钟

导语

这个中秋女友跟着小姐妹去旅游了,很遗憾没提前约好一起回家!

图片

只能孤零零的自己一个人回去老家了撒~BUT 女友一个人出门怎么会安全呢?

回家那天晚上,偷偷拿到女友手机打开了GPS定位系统,这样就不用担心啦~

你以为就是只打开GPS嘛?双重保险嘛!小编偷偷写了一款可以通过女友发过来的照片获取全球地址的定位系统哦!

是不是很期待想知道怎么做到滴~

开始解密哈往下看!!【文章需要一个对象,SO 随机创建的,这只是一个效果不要骂我——23333】

正文

为了女友不再走丢,今天带大家写一波定位系统!首先手机要打开GPS定位,这样子拍出来的照片才会带有GPS信息的哈!平常拍的照片里面,隐藏了超多私密的信息的,包括——拍照的时间、GPS信息定位。这里需要调用百度转换成地址哦!

环境安装:Python3.6、pycharm2021、以及自带的模块,你们可自行安排版本。

安装模块:

 pip install -i https://pypi.douban.com/simple/ requests

 pip install -i https://pypi.douban.com/simple/ exifread 

Python分析照片详细拍摄地点需要引入exifread模块,该模块用于分析照片信息,提取照片的GPS等信息。

(1) **读取照片的GPS经纬度信息,**转换经纬度格式。

def latitude_and_longitude_convert_to_decimal_system(*arg):
    """
    经纬度转为小数, param arg:
    :return: 十进制小数
    """
    return float(arg[0]) + ((float(arg[1]) + (float(arg[2].split('/')[0]) / float(arg[2].split('/')[-1]) / 60)) / 60)


def find_GPS_image(pic_path):
    GPS = {}
    date = ''
    with open(pic_path, 'rb') as f:
        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])
                except:
                    deg, min, sec = [x.replace(' ', '') for x in str(value)[1:-1].split(',')]
                    GPS['GPSLatitude'] = latitude_and_longitude_convert_to_decimal_system(deg, min, sec)
            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])
                except:
                    deg, min, sec = [x.replace(' ', '') for x in str(value)[1:-1].split(',')]
                    GPS['GPSLongitude'] = latitude_and_longitude_convert_to_decimal_system(deg, min, sec)
            elif re.match('GPS GPSAltitude', tag):
                GPS['GPSAltitude'] = str(value)
            elif re.match('.*Date.*', tag):
                date = str(value)
    return {'GPS_information': GPS, 'date_information': date}

**(2)**通过baidu Map的API将GPS信息转换成地址。

def find_address_from_GPS(GPS):
    """
    使用Geocoding API把经纬度坐标转换为结构化地址。
    :param GPS:
    :return:
    """
    secret_key = 'zbLsuDDL4CS2U0M4KezOZZbGUY9iWtVf'
    if not GPS['GPS_information']:
        return '该照片无GPS信息'
    lat, lng = GPS['GPS_information']['GPSLatitude'], GPS['GPS_information']['GPSLongitude']
    baidu_map_api = "http://api.map.baidu.com/geocoder/v2/?ak={0}&callback=renderReverse&location={1},{2}s&output=json&pois=0".format(
        secret_key, lat, lng)
    response = requests.get(baidu_map_api)
    content = response.text.replace("renderReverse&&renderReverse(", "")[:-1]
    print(content)
    baidu_map_address = json.loads(content)
    formatted_address = baidu_map_address["result"]["formatted_address"]
    province = baidu_map_address["result"]["addressComponent"]["province"]
    city = baidu_map_address["result"]["addressComponent"]["city"]
    district = baidu_map_address["result"]["addressComponent"]["district"]
    location = baidu_map_address["result"]["sematic_description"]
    return formatted_address, province, city, district, location

(3)得出照片的拍摄地址跟时间。

if __name__ == '__main__':
    GPS_info = find_GPS_image(pic_path='test.jpg')
    address = find_address_from_GPS(GPS=GPS_info)
    print("拍摄时间:" + GPS_info.get("date_information"))
    print('照片拍摄地址:' + str(address))

效果图如下:这是百度上面找的撒!所以没地址哈哈哈。假装.jpg 光明正大看小姐姐~

​总结

好啦!本次文章就写到这里啦!记得三连哦~爱你

完整的项目+素材打包好啦还是老规矩源码基地见:#私信小编06或者直接看评论区就知道在哪儿领取源码啦#