我们打开手机的GPS定位、拍照设置成保存地理位置、发送原图的时候,照片里存储的一些信息都会和照片一起传输给对方。如若担心泄漏个人位置信息,可以关闭定位或者P图。(PS:微信朋友圈发送的图片都经过了压缩,不带位置等信息。)

以下代码通过exifread获取照片Exif参数。

get_image_info.py内容:

import os
import exifread
import re
import requests

# GPS地址转换  (由[22, 34, 18141/1250]=>114.09728199999999)
def format_lati_long(data):
    list_tmp=str(data).replace('[', '').replace(']', '').split(',')
    list=[ele.strip() for ele in list_tmp]
    data_sec = int(list[-1].split('/')[0]) /(int(list[-1].split('/')[1])*3600)# 秒的值
    data_minute = int(list[1])/60
    data_degree = int(list[0])
    result=data_degree + data_minute + data_sec
    return result

# 请求腾讯地图逆地址解析接口
def get_address(lat, lng):
    url = "https://apis.map.qq.com/ws/geocoder/v1/"
    headers = {'content-type': 'application/json'}
    print(lat,lng)
    requestData = {"location": lat+','+lng, "key": "腾讯地图开发密钥(Key)"}
    ret = requests.post(url, json=requestData, headers=headers)
    print(str(ret.content,'utf-8'))

# 使用exifread获取图片信息图片
if __name__ =="__main__":
    try:
        with open("test1.jpg", 'rb') as f:
            imagetext = exifread.process_file(f)
            # for key in imagetext:  # 打印键值对
            #     print(key, ":", imagetext[key])
            lng = ''
            lat = ''
            for q in imagetext:  # 打印该图片的经纬度 以及拍摄的时间
                if q == "GPS GPSLongitude":
                    print("GPS经度 =", imagetext[q], imagetext['GPS GPSLatitudeRef'])
                    lng = imagetext[q]
                elif q == "GPS GPSLatitude":
                    print("GPS纬度 =", imagetext[q], imagetext['GPS GPSLongitudeRef'])
                    lat = imagetext[q]
                elif q == 'Image DateTime':
                    print("拍摄时间 =", imagetext[q])
            if lng and lat:
                lat = str(format_lati_long(lat))
                lng = str(format_lati_long(lng))
                get_address(lat, lng)
            else:
                print("没有图片地理位置信息")
    except FileNotFoundError:
        print("没有找到文件")

运行结果:
图片说明

根据ip获取地理位置信息(误差有些大)
get_client.py文件内容

import requests
from get_image_info import get_address
import json

def get_client_ip(ip):
    url = "https://apis.map.qq.com/ws/location/v1/ip/"
    headers = {'content-type': 'application/json'}
    requestData = {"ip": ip, "key": "腾讯地图开发密钥(Key)"}
    ret = requests.get(url, params=requestData, headers=headers)
    print(str(ret.content,"utf-8"))
    return ret.content

if __name__ == "__main__":
    ip = "113.118.4.85"
    result = get_client_ip(ip)
    print("---------------------------------------------------------")
    response_content = json.loads(result)
    if int(response_content['status']) == 0:
        get_address(str(response_content['result']['location']['lat']),str(response_content['result']['location']['lng']))

运行结果:
图片说明