用到 exiread 得到图片中的 定位信息
匹配记录
调用 百度 api 得到地址
baidu_map_api ="<http://api.map.baidu.com/reverse_geocoding/v3/?location={0},{1>}&output=json&pois=1&ak={2}".format(lat, lng, secret_key)content = requests.get(baidu_map_api).textgps_address = json.loads(content)
# -*- coding:utf-8 -*-
"""
0xc4m3l
2019.9.2
"""
import os
import exifread
import re
import sys
import requests
import json
ak = 'wfHneTY3mu26nPtHePyEoNlDlBpp1AGk'
class MetaPicture(object):
# 类变量, 图片文件的 绝对路径
picture_paths = sys.argv[1] #os.path.join(os.path.dirname(os.path.abspath(__file__)), 'picture') # 找当前目录的 picture 文件夹里面的图片
def read_picture(self):
self.get_picture_exif(self.__class__.picture_paths)
# 读取图片,并调用自身提取元 数据的方法
#pictures = os.listdir(self.__class__.picture_paths) # os.listdir 返回 picture_paths 地址的文件或文件夹的名字
#for picture in pictures:
# picture_patch = os.path.join(self.picture_paths, picture) # picture_patch 为对应的 picture 的绝对地址
# self.get_picture_exif(picture_patch) # 调用 get_picture_exif 函数
def get_picture_exif(self, picture_name):
img_file = open(picture_name, 'rb') # 打开图片
picture_info = exifread.process_file(img_file) # 调用 exifread 的 process_file 函数 返回图片的 exif 信息
if picture_info:
GPSLatitude = picture_info['GPS GPSLatitude'] # 纬度数
GPSLatitudeRef = picture_info['GPS GPSLatitudeRef'] # N,S 南北纬
GPSLongitude = picture_info['GPS GPSLongitude'] # 经度数
GPSLongitudeRef = picture_info['GPS GPSLongitudeRef'] # E,W 东西经
GPSDate = picture_info['EXIF DateTimeOriginal'] # 拍摄时间
if GPSLatitude and GPSLongitude and GPSDate: # 图片有经纬度和拍摄时间 就显示 这些信息
print('纬度:{}{}\\n精度:{}{}\\n拍摄时间:{}\\n'.format(GPSLatitudeRef, GPSLatitude, GPSLongitudeRef, GPSLongitude, GPSDate))
lng = str(GPSLongitude)
deg, minu, sec = [x.replace(' ', '') for x in lng[1:-1].split(',')]
lng = float(deg) + float(minu)/float(60) + (float)(eval(sec))/(float)(3600)
lat = str(GPSLatitude)
deg, minu, sec = [x.replace(' ', '') for x in lat[1:-1].split(',')]
lat = float(deg) + float(minu) / float(60) + (float)(eval(sec)) / (float)(3600)
latitude = self.deal_data_format(GPSLatitude)
longitude = self.deal_data_format(GPSLongitude)
print('处理后的经纬度:【{}{},{}{}】'.format(GPSLatitudeRef, latitude, GPSLongitudeRef, longitude))
secret_key = 'tAM6QlkxIS41jOIvkLcAGAPXaVaR3GGa'
#baidu_map_api = "<http://api.map.baidu.com/reverse_geocoding/v3/?ak={0}&output=json&coordtype=wgs84ll&location={1},{2>}".format(secret_key, lat, lng)
baidu_map_api ="<http://api.map.baidu.com/reverse_geocoding/v3/?location={0},{1>}&output=json&pois=1&ak={2}".format(lat, lng, secret_key)
content = requests.get(baidu_map_api).text
gps_address = json.loads(content)
formatted_address = gps_address["result"]["formatted_address"] # 结构化地址
country = gps_address["result"]["addressComponent"]["country"] # 国家地址
province = gps_address["result"]["addressComponent"]["province"] # 省地址
city = gps_address["result"]["addressComponent"]["city"] # 城市
district = gps_address["result"]["addressComponent"]["district"] # 区
sematic_description = gps_address["result"]["sematic_description"] # 语义化地址描述
print ("{}|{}|{}|{}|{}".format(formatted_address, country, province, city, district, sematic_description))
else:
print('请检查图片是否是原图!')
def deal_data_format(self, data):
""" 处理数据,清洗格式生成对应内容 """
data_list_tmp = str(data).replace('[', '').replace(']', '').split(',')
data_list = [data.strip() for data in data_list_tmp]
data_tmp = data_list[-1].split('/')
data_list[-1] = str(int(data_tmp[0]) / int(data_tmp[1]))
# 为了适配gps定位网站的规格输出,将列表做成度分时的状态
data_list.insert(1, '°')
data_list.insert(3, '′′')
data_str = ''.join(data_list)
return data_str
def main():
metaPicture = MetaPicture()
metaPicture.read_picture()
main()
运行脚本后