Python-根據照片信息獲取用戶詳細信息(微信發原圖或泄露位置信息)

前言

有媒體曝出,微信發原圖或存在泄露位置信息的風險。
對此,騰訊微信團隊微博12月1日發佈聲明稱,朋友圈發送的照片都通過系統自動壓縮,不帶位置等信息,實在擔憂的話,能夠P完圖再發,以下圖:node

微信團隊提到過Exif,何爲Exif?git

可交換圖像文件格式(英語:Exchangeable image file format,官方簡稱Exif),是專門爲數碼相機的照片設定的,能夠記錄數碼照片的屬性信息和拍攝數據。json

Exif最初由日本電子工業發展協會在1996年制定,版本爲1.0。1998年,升級到2.1,增長了對音頻文件的支持。2002年3月,發表了2.2版。bash

👉詳細請見百度百科《Exif》baike.baidu.com/item/Exif/4…微信

Python庫

這裏須要Python的兩個庫,一個是讀取Exif信息的exifread;一個是根據經緯度獲取詳細地址信息的geopy;post

安裝以下:ui

pip3 install exifread

pip3 install geopy複製代碼

Python源碼

import exifread
import json
import urllib.request
import sys
from geopy.geocoders import Nominatim

# 獲取照片的詳細信息
def get_img_infor_tup(photo):
    img_file = open(photo, 'rb')
    image_map = exifread.process_file(img_file)

    try:
        #圖片的經度
        img_longitude_ref = image_map["GPS GPSLongitudeRef"].printable
        img_longitude = image_map["GPS GPSLongitude"].printable[1:-1].replace(" ","").replace("/",",").split(",")
        img_longitude = float(img_longitude[0])+float(img_longitude[1])/60+float(img_longitude[2])/float(img_longitude[3])/3600
        if img_longitude_ref != "E":
            img_longitude = img_longitude * (-1)

        #圖片的緯度
        img_latitude_ref = image_map["GPS GPSLatitudeRef"].printable
        img_latitude = image_map["GPS GPSLatitude"].printable[1:-1].replace(" ","").replace("/",",").split(",")
        img_latitude = float(img_latitude[0])+float(img_latitude[1])/60+float(img_latitude[2])/float(img_latitude[3])/3600
        if img_latitude_ref != "N":
            img_latitude = img_latitude*(-1)

        #照片拍攝時間
        img_create_date = image_map["EXIF DateTimeOriginal"].printable

        img_file.close()

        # 返回經緯度元組
        return img_longitude, img_latitude, img_create_date

    except Exception as e:
        print('ERROR:圖片中不包含Gps信息')

# 根據經緯度獲取詳細的信息
def get_detail_infor(lat, lon):
    reverse_value = str(lat) + ', ' + str(lon)
    geolocator = Nominatim()
    location = geolocator.reverse(reverse_value)

    print('照片的經緯度信息:')
    print((location.latitude, location.longitude))

    print('照片的地址信息:')
    print(location.address)
    
    print('照片的所有信息:')
    print(location.raw)

if __name__ == '__main__':
    infor_tup = get_img_infor_tup('./image/IMG_2174.JPG')
    get_detail_infor(infor_tup[1], infor_tup[0])
    
複製代碼

運行結果

照片的經緯度信息:
(31.2734692, 121.4653229)

照片的地址信息:
Appart Jeje, 45, 柳營路, 卓悅局, 靜安區, 上海市, 200072, China 中國

照片的所有信息:
{'place_id': 245107137, 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright', 'osm_type': 'node', 'osm_id': 6066843985, 'lat': '31.2734692', 'lon': '121.4653229', 'display_name': 'Appart Jeje, 45, 柳營路, 卓悅局, 靜安區, 上海市, 200072, China 中國', 'address': {'address29': 'Appart Jeje', 'house_number': '45', 'road': '柳營路', 'neighbourhood': '卓悅局', 'city': '靜安區', 'county': '靜安區', 'state': '上海市', 'postcode': '200072', 'country': 'China 中國', 'country_code': 'cn'}, 'boundingbox': ['31.2733692', '31.2735692', '121.4652229', '121.4654229']}
複製代碼

結束語

Exif針對因此的原圖照片,因此在發照片的時候若是不想我的信息被泄露,能夠發壓縮過得圖片和PS過得圖片,須要說明的一點是經過微信發照片是默認壓縮的!url

歡迎各位大神提出寶貴的意見和建議,也歡迎你們看左側掃碼進羣365152048交流!spa

相關文章
相關標籤/搜索