|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +import json |
| 4 | +import re |
| 5 | + |
| 6 | +import exifread |
| 7 | +import requests |
| 8 | + |
| 9 | + |
| 10 | +#转换经纬度格式 |
| 11 | +def convert_coor(*arg): |
| 12 | + return float(arg[0]) + ((float(arg[1]) + (float(arg[2].split('/')[0]) / float(arg[2].split('/')[-1]) / 60)) / 60) |
| 13 | + |
| 14 | +# 读取照片的GPS经纬度信息 |
| 15 | +def extract_image(pic_path): |
| 16 | + GPS = {} |
| 17 | + date = '' |
| 18 | + with open(pic_path, 'rb') as f: |
| 19 | + tags = exifread.process_file(f) |
| 20 | + for tag, value in tags.items(): |
| 21 | + # 纬度 |
| 22 | + if re.match('GPS GPSLatitudeRef', tag): |
| 23 | + GPS['GPSLatitudeRef'] = str(value) |
| 24 | + # 经度 |
| 25 | + elif re.match('GPS GPSLongitudeRef', tag): |
| 26 | + GPS['GPSLongitudeRef'] = str(value) |
| 27 | + # 海拔 |
| 28 | + elif re.match('GPS GPSAltitudeRef', tag): |
| 29 | + GPS['GPSAltitudeRef'] = str(value) |
| 30 | + elif re.match('GPS GPSLatitude', tag): |
| 31 | + try: |
| 32 | + match_result = re.match('\[(\w*),(\w*),(\w.*)/(\w.*)\]', str(value)).groups() |
| 33 | + GPS['GPSLatitude'] = int(match_result[0]), int(match_result[1]), int(match_result[2]) |
| 34 | + except: |
| 35 | + deg, min, sec = [x.replace(' ', '') for x in str(value)[1:-1].split(',')] |
| 36 | + GPS['GPSLatitude'] = convert_coor(deg, min, sec) |
| 37 | + elif re.match('GPS GPSLongitude', tag): |
| 38 | + try: |
| 39 | + match_result = re.match('\[(\w*),(\w*),(\w.*)/(\w.*)\]', str(value)).groups() |
| 40 | + GPS['GPSLongitude'] = int(match_result[0]), int(match_result[1]), int(match_result[2]) |
| 41 | + except: |
| 42 | + deg, min, sec = [x.replace(' ', '') for x in str(value)[1:-1].split(',')] |
| 43 | + GPS['GPSLongitude'] = convert_coor(deg, min, sec) |
| 44 | + elif re.match('GPS GPSAltitude', tag): |
| 45 | + GPS['GPSAltitude'] = str(value) |
| 46 | + elif re.match('.*Date.*', tag): |
| 47 | + date = str(value) |
| 48 | + return {'GPS_information': GPS, 'date_information': date} |
| 49 | + |
| 50 | + |
| 51 | +# 通过baidu Map的API将GPS信息转换成地址 |
| 52 | +def find_address_from_bd(GPS): |
| 53 | + secret_key = 'wLyevcXk5QY36hTKmvV5350F' |
| 54 | + if not GPS['GPS_information']: |
| 55 | + return '该照片无GPS信息' |
| 56 | + lat, lng = GPS['GPS_information']['GPSLatitude'], GPS['GPS_information']['GPSLongitude'] |
| 57 | + baidu_map_api = "http://api.map.baidu.com/geocoder/v2/?ak={0}&callback=renderReverse&location={1},{2}s&output=json&pois=0".format( |
| 58 | + secret_key, lat, lng) |
| 59 | + response = requests.get(baidu_map_api) |
| 60 | + content = response.text.replace("renderReverse&&renderReverse(", "")[:-1] |
| 61 | + baidu_map_address = json.loads(content) |
| 62 | + formatted_address = baidu_map_address["result"]["formatted_address"] |
| 63 | + province = baidu_map_address["result"]["addressComponent"]["province"] |
| 64 | + city = baidu_map_address["result"]["addressComponent"]["city"] |
| 65 | + district = baidu_map_address["result"]["addressComponent"]["district"] |
| 66 | + location = baidu_map_address["result"]["sematic_description"] |
| 67 | + return formatted_address, province, city, district, location |
| 68 | + |
| 69 | + |
| 70 | +if __name__ == '__main__': |
| 71 | + gpsinfo = extract_image(pic_path='/Users/xxx/Pictures/66.jpg') |
| 72 | + address = find_address_from_bd(GPS=gpsinfo) |
| 73 | + print(gpsinfo) |
| 74 | + print("拍摄时间:" + gpsinfo.get("date_information")) |
| 75 | + print('照片拍摄地址:' + str(address)) |
0 commit comments