女朋友背着我,用 Python 偷偷隐藏了她的行踪

开发 后端
女朋友会 Python 是多么可怕的一件事!上篇文章发布之后,一位朋友告诉忽略了一件事,假设女朋友会 Python 的话,那岂不是要翻车?如果是这样的话,女朋友发过来的图片包含的地理位置是否真实,要打一个 Big Big 的问号了?

1.目标场景

女朋友会 Python 是多么可怕的一件事!

[[282840]]

上篇文章发布之后,一位朋友告诉忽略了一件事,假设女朋友会 Python 的话,那岂不是要翻车?如果是这样的话,女朋友发过来的图片包含的地理位置是否真实,要打一个 Big Big 的问号了?

实际上,利用 Python 是可以非常方便地修改一张图片的地理位置。

2.编写代码

在编写脚本之前,需要在虚拟环境下安装依赖库:piexif

第一步,我们读取原图的经度和纬度,然后将地理位置格式化为浮点类型。

拿到 GPS 图片坐标之后,需要转为火星坐标系数据。

 

  1. def read_image(self, image_path): 
  2.     ""
  3.     开始处理图片 
  4.     exifread:读取图片属性 
  5.     :return
  6.     ""
  7.     exif_dict = piexif.load(image_path) 
  8.  
  9.     if exif_dict['GPS']: 
  10.  
  11.          # 纬度 
  12.          gps_lati_pre = exif_dict['GPS'][2] 
  13.  
  14.          gps_lati = dms_to_gps(gps_lati_pre) 
  15.  
  16.          # 经度 
  17.          gps_long_pre = exif_dict['GPS'][4] 
  18.          gps_long = dms_to_gps(gps_long_pre) 
  19.  
  20.          # GPS坐标转为高德坐标 
  21.          lng, lat = wgs84togcj02(gps_long, gps_lati) 
  22.  
  23.          print(f"原图地理位置如下\n经度:{lng}\n纬度:{lat}\n"
  24.  
  25.          return f'{lng}, {lat}' 
  26.     else
  27.          print(f'抱歉!这张图片不包含地理位置!'

第二步,利用高德 Web 服务 API 中的 逆地理编码 解析出原图的详细位置。

 

  1. def get_address_by_location(self, location): 
  2.     ""
  3.     通过经纬度拿到地理位置 
  4.     :param location: 
  5.     :return
  6.     ""
  7.     params = { 
  8.        'key': self.ak, 
  9.         'location': location, 
  10.         'sig': self.sign 
  11.     } 
  12.  
  13.     resp = json.loads(requests.get(url=self.url_regeo, params=params).text) 
  14.  
  15.     if resp and resp.get('regeocode'and resp.get('regeocode').get('formatted_address'): 
  16.          address = resp.get('regeocode').get('formatted_address'
  17.          print(f'原图的拍摄地址为:{address}\n'
  18.     else
  19.          print('api解析地址出错,请检查ak!\n'

第三步,寻找一个要定位的地理位置。

首先,输入目标城市和具体地址,利用高德 Web 服务 API 中的地理编码拿到要定位的地理位置。

 

  1. def get_location_by_address(self, city, address): 
  2.     ""
  3.     通过地理位置到拿到经纬度 
  4.     地理编码:https://lbs.amap.com/api/webservice/guide/api/georegeo/ 
  5.     :param address: 
  6.     :return
  7.     ""
  8.     params = { 
  9.             'key': self.ak, 
  10.             'city': city, 
  11.             'address': address, 
  12.             'sig': self.sign 
  13.     } 
  14.  
  15.     resp = json.loads(requests.get(url=self.url_geo, params=params).text) 
  16.  
  17.     # 获取坐标地址 
  18.     if resp and len(resp.get('geocodes')) >= 1 and resp.get('geocodes')[0].get('location'): 
  19.          location = resp.get('geocodes')[0].get('location'
  20.          gps_data = location.split(','
  21.  
  22.          # 得到经度和纬度 
  23.          gps_long = float(gps_data[0]) 
  24.          gps_lati = float(gps_data[1]) 
  25.  
  26.          return gps_long, gps_lati 
  27.     else
  28.          print('api解析地址出错,请检查ak!'
  29.          return None 

第四步,修改图片的地理位置。

由于 piexif 中经、纬度数据都是元组类型,因此需要将要设置的数据进行一次处理。

 

  1. def gps_to_dms(gps_data): 
  2.     ""
  3.     坐标转为度、分、秒(double
  4.     116.397451 
  5.     http://www.gzhatu.com/du2dfm.html 
  6.     :param gps_data: 
  7.     :return:{1: b'N', 2: ((22, 1), (32, 1), (945, 100)), 3: b'E', 4: ((114, 1), (1, 1), (3445, 100)) 
  8.     ""
  9.     # 度:向下取整 
  10.     gps_degree = math.floor(gps_data) 
  11.  
  12.     gps_data_temp1 = (gps_data - gps_degree) * 60 
  13.  
  14.     # 分 
  15.     gps_minute = math.floor(gps_data_temp1) 
  16.  
  17.     gps_data_temp2 = gps_data_temp1 - gps_minute 
  18.  
  19.     # 秒,取小数点后4位 
  20.     gps_second = round(gps_data_temp2 * 60, 2) 
  21.  
  22.     # 注意:秒必须转换为整形 
  23.     result = ((gps_degree, 1), (gps_minute, 1), (int(gps_second * 100), 100)) 
  24.  
  25.     return result 

最后,将正确格式的经纬度数据写入到图片中去。

 

  1. def write_image(self, image_path, gps_long, gps_lati): 
  2.     ""
  3.     修改文件夹下所有文件的属性 
  4.     :param image_path: 文件夹路径 
  5.     :return
  6.     ""
  7.     # 读取图片 
  8.     img = Image.open(image_path) 
  9.  
  10.     try: 
  11.         exif_dict = piexif.load(img.info['exif']) 
  12.     except
  13.         print('加载文件地理位置异常!'
  14.         return 
  15.  
  16.     # 修改地理位置 
  17.     # GPS GPSLatitudeRef:N 
  18.     # GPS GPSLatitude:[22, 32, 189/20] 
  19.     # GPS GPSLongitudeRef:E 
  20.     # GPS GPSLongitude:[114, 1, 689/20] 
  21.     exif_dict['GPS'][2] = gps_to_dms(gps_lati) 
  22.     exif_dict['GPS'][4] = gps_to_dms(gps_long) 
  23.  
  24.     exif_bytes = piexif.dump(exif_dict) 
  25.  
  26.     # 写入到新的图片中去 
  27.     img.save(image_path, 'jpeg', exif=exif_bytes) 

3.结果结论

通过上面的 4 步操作,能将任意地理位置写入到图片中。

 

 

 

 

除了地理位置,图片的元数据,包含:拍摄器材、拍摄时间、拍摄参数都能进行二次修改。

责任编辑:华轩 来源: AirPython
相关推荐

2019-08-28 16:22:30

Python数据微信

2019-10-24 09:29:04

程序员程序员节女朋友

2019-09-24 11:36:51

Python支付宝数据

2019-08-12 10:27:34

前端程序员网络

2019-07-09 09:19:51

分布式事务App

2022-09-17 08:10:20

HSV饱和度图像

2021-05-19 11:02:44

PythonTurtle参数

2021-04-06 06:23:18

MVCC并发事务

2022-01-02 07:02:48

硬盘保护文件GiliSoft Fi

2020-01-02 09:14:23

Kubernetes内部容器

2021-05-14 07:18:07

监控微信聊天

2019-05-28 19:05:02

Python女朋友机器人

2021-08-11 14:33:38

注解springAutowired

2014-08-21 08:59:44

2015-08-26 10:17:29

程序员女朋友

2021-02-20 07:52:35

防猝死插件 IDEA

2021-02-02 11:59:15

插件开发工具

2021-03-03 09:16:51

容器技术容器云计算

2020-04-21 11:45:04

技巧单一责任链开闭原则

2020-05-13 18:10:47

个人隐私App移动应用
点赞
收藏

51CTO技术栈公众号