女朋友背著我,用 Python 偷偷隱藏了她的行蹤
1.目標(biāo)場景
女朋友會 Python 是多么可怕的一件事!
上篇文章發(fā)布之后,一位朋友告訴忽略了一件事,假設(shè)女朋友會 Python 的話,那豈不是要翻車?如果是這樣的話,女朋友發(fā)過來的圖片包含的地理位置是否真實(shí),要打一個(gè) Big Big 的問號了?
實(shí)際上,利用 Python 是可以非常方便地修改一張圖片的地理位置。
2.編寫代碼
在編寫腳本之前,需要在虛擬環(huán)境下安裝依賴庫:piexif
第一步,我們讀取原圖的經(jīng)度和緯度,然后將地理位置格式化為浮點(diǎn)類型。
拿到 GPS 圖片坐標(biāo)之后,需要轉(zhuǎn)為火星坐標(biāo)系數(shù)據(jù)。
- def read_image(self, image_path):
- """
- 開始處理圖片
- exifread:讀取圖片屬性
- :return:
- """
- exif_dict = piexif.load(image_path)
- if exif_dict['GPS']:
- # 緯度
- gps_lati_pre = exif_dict['GPS'][2]
- gps_lati = dms_to_gps(gps_lati_pre)
- # 經(jīng)度
- gps_long_pre = exif_dict['GPS'][4]
- gps_long = dms_to_gps(gps_long_pre)
- # GPS坐標(biāo)轉(zhuǎn)為高德坐標(biāo)
- lng, lat = wgs84togcj02(gps_long, gps_lati)
- print(f"原圖地理位置如下\n經(jīng)度:{lng}\n緯度:{lat}\n")
- return f'{lng}, {lat}'
- else:
- print(f'抱歉!這張圖片不包含地理位置!')
第二步,利用高德 Web 服務(wù) API 中的 逆地理編碼 解析出原圖的詳細(xì)位置。
- def get_address_by_location(self, location):
- """
- 通過經(jīng)緯度拿到地理位置
- :param location:
- :return:
- """
- params = {
- 'key': self.ak,
- 'location': location,
- 'sig': self.sign
- }
- resp = json.loads(requests.get(url=self.url_regeo, params=params).text)
- if resp and resp.get('regeocode') and resp.get('regeocode').get('formatted_address'):
- address = resp.get('regeocode').get('formatted_address')
- print(f'原圖的拍攝地址為:{address}\n')
- else:
- print('api解析地址出錯,請檢查ak!\n')
第三步,尋找一個(gè)要定位的地理位置。
首先,輸入目標(biāo)城市和具體地址,利用高德 Web 服務(wù) API 中的地理編碼拿到要定位的地理位置。
- def get_location_by_address(self, city, address):
- """
- 通過地理位置到拿到經(jīng)緯度
- 地理編碼:https://lbs.amap.com/api/webservice/guide/api/georegeo/
- :param address:
- :return:
- """
- params = {
- 'key': self.ak,
- 'city': city,
- 'address': address,
- 'sig': self.sign
- }
- resp = json.loads(requests.get(url=self.url_geo, params=params).text)
- # 獲取坐標(biāo)地址
- if resp and len(resp.get('geocodes')) >= 1 and resp.get('geocodes')[0].get('location'):
- location = resp.get('geocodes')[0].get('location')
- gps_data = location.split(',')
- # 得到經(jīng)度和緯度
- gps_long = float(gps_data[0])
- gps_lati = float(gps_data[1])
- return gps_long, gps_lati
- else:
- print('api解析地址出錯,請檢查ak!')
- return None
第四步,修改圖片的地理位置。
由于 piexif 中經(jīng)、緯度數(shù)據(jù)都是元組類型,因此需要將要設(shè)置的數(shù)據(jù)進(jìn)行一次處理。
- def gps_to_dms(gps_data):
- """
- 坐標(biāo)轉(zhuǎn)為度、分、秒(double)
- 116.397451
- http://www.gzhatu.com/du2dfm.html
- :param gps_data:
- :return:{1: b'N', 2: ((22, 1), (32, 1), (945, 100)), 3: b'E', 4: ((114, 1), (1, 1), (3445, 100))
- """
- # 度:向下取整
- gps_degree = math.floor(gps_data)
- gps_data_temp1 = (gps_data - gps_degree) * 60
- # 分
- gps_minute = math.floor(gps_data_temp1)
- gps_data_temp2 = gps_data_temp1 - gps_minute
- # 秒,取小數(shù)點(diǎn)后4位
- gps_second = round(gps_data_temp2 * 60, 2)
- # 注意:秒必須轉(zhuǎn)換為整形
- result = ((gps_degree, 1), (gps_minute, 1), (int(gps_second * 100), 100))
- return result
最后,將正確格式的經(jīng)緯度數(shù)據(jù)寫入到圖片中去。
- def write_image(self, image_path, gps_long, gps_lati):
- """
- 修改文件夾下所有文件的屬性
- :param image_path: 文件夾路徑
- :return:
- """
- # 讀取圖片
- img = Image.open(image_path)
- try:
- exif_dict = piexif.load(img.info['exif'])
- except:
- print('加載文件地理位置異常!')
- return
- # 修改地理位置
- # GPS GPSLatitudeRef:N
- # GPS GPSLatitude:[22, 32, 189/20]
- # GPS GPSLongitudeRef:E
- # GPS GPSLongitude:[114, 1, 689/20]
- exif_dict['GPS'][2] = gps_to_dms(gps_lati)
- exif_dict['GPS'][4] = gps_to_dms(gps_long)
- exif_bytes = piexif.dump(exif_dict)
- # 寫入到新的圖片中去
- img.save(image_path, 'jpeg', exif=exif_bytes)
3.結(jié)果結(jié)論
通過上面的 4 步操作,能將任意地理位置寫入到圖片中。
除了地理位置,圖片的元數(shù)據(jù),包含:拍攝器材、拍攝時(shí)間、拍攝參數(shù)都能進(jìn)行二次修改。