Python exifread

Python利用exifread库来解析照片的经纬度,对接百度地图API显示拍摄地点。

import exifread

import re

import json

import requests

def latitude_and_longitude_convert_to_decimal_system(*arg):

"""

经纬度转为小数, 作者尝试适用于iphone6、ipad2以上的拍照的照片,

:param arg:

:return: 十进制小数

"""

return float(arg[0]) + ((float(arg[1]) + (float(arg[2].split('/')[0]) / float(arg[2].split('/')[-1]) / 60)) / 60)

def find_GPS_image(pic_path):

GPS = {}

date = ''

with open(pic_path, 'rb') as f:

tags = exifread.process_file(f)

for tag, value in tags.items():

if re.match('GPS GPSLatitudeRef', tag):

GPS['GPSLatitudeRef'] = str(value)

elif re.match('GPS GPSLongitudeRef', tag):

GPS['GPSLongitudeRef'] = str(value)

elif re.match('GPS GPSAltitudeRef', tag):

GPS['GPSAltitudeRef'] = str(value)

elif re.match('GPS GPSLatitude', tag):

try:

match_result = re.match('\[(\w*),(\w*),(\w.*)/(\w.*)\]', str(value)).groups()

GPS['GPSLatitude'] = int(match_result[0]), int(match_result[1]), int(match_result[2])

except:

deg, min, sec = [x.replace(' ', '') for x in str(value)[1:-1].split(',')]

GPS['GPSLatitude'] = latitude_and_longitude_convert_to_decimal_system(deg, min, sec)

elif re.match('GPS GPSLongitude', tag):

try:

match_result = re.match('\[(\w*),(\w*),(\w.*)/(\w.*)\]', str(value)).groups()

GPS['GPSLongitude'] = int(match_result[0]), int(match_result[1]), int(match_result[2])

except:

deg, min, sec = [x.replace(' ', '') for x in str(value)[1:-1].split(',')]

GPS['GPSLongitude'] = latitude_and_longitude_convert_to_decimal_system(deg, min, sec)

elif re.match('GPS GPSAltitude', tag):

GPS['GPSAltitude'] = str(value)

elif re.match('.*Date.*', tag):

date = str(value)

return {'GPS_information': GPS, 'date_information': date}

def find_address_from_GPS(GPS):

print(GPS)

"""

使用Geocoding API把经纬度坐标转换为结构化地址。

:param GPS:

:return:

"""

secret_key = 'xxxxxxxxxxxxxxxxxxxx' # 百度地图创应用的秘钥

if not GPS['GPS_information']:

return '该照片无GPS信息'

lat, lng = GPS['GPS_information']['GPSLatitude'], GPS['GPS_information']['GPSLongitude']

baidu_map_api = "http://api.map.baidu.com/geocoder/v2/?ak={0}&callback=renderReverse&location={1},{2}s&output=json&pois=0".format(

secret_key, lat, lng)

response = requests.get(baidu_map_api)

content = response.text.replace("renderReverse&&renderReverse(", "")[:-1]

baidu_map_address = json.loads(content)

formatted_address = baidu_map_address["result"]["formatted_address"]

# province = baidu_map_address["result"]["addressComponent"]["province"]

# city = baidu_map_address["result"]["addressComponent"]["city"]

# district = baidu_map_address["result"]["addressComponent"]["district"]

return formatted_address

GPS_info = find_GPS_image(pic_path='lllll.jpg') # 照片

address = find_address_from_GPS(GPS=GPS_info)

print(address)

python读取图片路径_Python小列子-读取照片位置相关推荐

  1. mac系统python读取文件路径_Python小技巧:3个处理文件路径的简单方法

    原标题 | Python 3 Quick Tip: The easy way to deal with file paths on Windows, Mac and Linux 作者 | Adam G ...

  2. python做图片美化_Python实现简单的照片磨皮(照片智能磨皮) 最新免费版

    Python实现简单的照片磨皮(照片智能磨皮)是一款用python写的最好的照片磨皮软件.需要配置opencv和numpy,使用的时候地址需要加英文的引号,斜杠要用双斜杠,例如"D:\\a. ...

  3. python获取图片坐标_python提取照片坐标信息的实例代码

    python提取照片坐标信息的代码如下所示: from PIL import Image from PIL.ExifTags import TAGS import os output="Z: ...

  4. java读取xml路径问题_java 中读取xml 和绝对路径

    一.得到绝对路径 本人用到的不多,就列举常用的. 以上2种可读取固定配置文件可用得到绝对路径 1.System.getProperty("user.dir"); 这个东西局限性十分 ...

  5. python读取图片文件名_python图像处理基础之PIL和opencv

    python图像处理基础 pip install pillow 安装PIL库 1.读取图片 from PIL import Image import numpy as np #打开图片 img = I ...

  6. python显示图片列表_python读取图片任意范围区域

    使用python进行图片处理,现在需要读出图片的任意一块区域,并将其转化为一维数组,方便后续卷积操作的使用. 下面使用两种方法进行处理: convert 函数 from PIL import Imag ...

  7. c语言使用小技巧之读取图片路径和label

    主要是我记性不好 记下这些常用的以免自己忘记. 1. 每个图片路径后面跟一个label 那么读取方式为:  fstream fin; fin.open("ss.txt", ios: ...

  8. python导入图片数据_Python中读取图片的6种方式

    Python进行图片处理,第一步就是读取图片,这里给大家整理了6种图片的读取方式,并将读取的图片装换成numpy.ndarray()格式.首先需要准备一张照片,假如你有女朋友的话,可以用女朋友的,没有 ...

  9. python读取图片格式_Python读取图片尺寸、图片格式

    Python读取图片尺寸.图片格式 需要用到PIL模块,使用pip安装Pillow.Pillow是从PIL fork过来的Python 图片库. from PIL import Image im = ...

最新文章

  1. uwsgi 安装报错 plugins/python/uwsgi_python.h:2:20: fatal error: Python.h: No such file or directory
  2. hadoop集群_Ambari搭建hadoop集群
  3. linux 的 usr 文件
  4. 遗传算法及其应用实现
  5. 自动化运维之–Cobbler
  6. runtime批处理mysql导出_【原】使用批处理BAT文件处理Mysql数据库 | 学步园
  7. db link的查看创建与删除
  8. 手动触发事件_HBase中MemStore的刷写触发机制
  9. ext3日志模式---文件系统
  10. smart-tax慧穗数字科技|企业财税数字化转型
  11. Blender3.0一些建模技巧与应用
  12. win10文件资源管理器默认打开我的电脑及左侧导航设置
  13. 使用 Moment.js 吧时间戳生成格式化时间
  14. Python: 鲁卡斯队列
  15. VScode运行时提示找不到应用程序
  16. Android画图demo
  17. 人际交往三个常见问题
  18. android9手机电池管家,电池寿命修复大师手机版
  19. 跑路、清退or出海?这道留给交易所的题太难
  20. 快速定量,Abbkine 蛋白质定量试剂盒BCA法来了!

热门文章

  1. javaScript设计模式-创建型设计模式
  2. 2019全球人工智能产品应用博览会将于今年5月在苏州举办
  3. 新华保险公司怎么样?
  4. 生活:与小周一起参加广运会
  5. bzoj3219 巡游
  6. 深圳Java培训:Java中的国际化
  7. ## C51单片机2种方法让8个流水灯依次亮灭
  8. 易宝接口callback
  9. XNA游戏:各种输入测试 中
  10. 基于SSH的新农村农舍养殖管理系统开发