python提取照片坐标信息的实例代码


Posted in Python onAugust 14, 2019

python提取照片坐标信息的代码如下所示:

from PIL import Image
from PIL.ExifTags import TAGS
import os
output="Z://result.csv"
out=open(output,'a')
out.write('lat,lon\n')
fpath="Z://iphonephoto"
for item in os.walk(fpath):
 ob=item[2]
 for i in ob:
  name=fpath+'/'+str(i)
  ret={}
  try:
   img=Image.open(name)
   if hasattr(img,'_getexif'):
    exifinfo=img._getexif()
    if exifinfo !=None:
     for tag,value in exifinfo.items():
      decoded=TAGS.get(tag,tag)
      ret[decoded]=value
      N1 = ret['GPSInfo'][2][0][0]
      N2 = ret['GPSInfo'][2][1][0]
      N3 = ret['GPSInfo'][2][2][0]
      N=int(N1)+int(N2)*(1.0/60)+int(N3)*(1.0/360000)
      E1 = ret['GPSInfo'][4][0][0]
      E2 = ret['GPSInfo'][4][1][0]
      E3 = ret['GPSInfo'][4][2][0]
      E=int(E1)+int(E2)*(1.0/60)+int(E3)*(1.0/360000)
      out.write(str(N)+','+str(E)+'\n')
  except:
   pass
out.close()

PS:Python小列子-读取照片位置

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 相关文章推荐
python中文乱码的解决方法
Nov 04 Python
python调用java的Webservice示例
Mar 10 Python
python django 增删改查操作 数据库Mysql
Jul 27 Python
python3写爬取B站视频弹幕功能
Dec 22 Python
python学习笔记--将python源文件打包成exe文件(pyinstaller)
May 26 Python
django进阶之cookie和session的使用示例
Aug 17 Python
python3.4控制用户输入与输出的方法
Oct 17 Python
一篇文章搞懂Python的类与对象名称空间
Dec 10 Python
浅谈python3发送post请求参数为空的情况
Dec 28 Python
Python3.5迭代器与生成器用法实例分析
Apr 30 Python
tensorflow指定CPU与GPU运算的方法实现
Apr 21 Python
python3.6.8 + pycharm + PyQt5 环境搭建的图文教程
Jun 11 Python
python2使用bs4爬取腾讯社招过程解析
Aug 14 #Python
详解用python计算阶乘的几种方法
Aug 14 #Python
Python使用scrapy爬取阳光热线问政平台过程解析
Aug 14 #Python
用Python抢火车票的简单小程序实现解析
Aug 14 #Python
Python定时任务随机时间执行的实现方法
Aug 14 #Python
查看Python依赖包及其版本号信息的方法
Aug 13 #Python
使用python实现unix2dos和dos2unix命令的例子
Aug 13 #Python
You might like
PHP 自定义错误处理函数trigger_error()
2013/03/26 PHP
php接口实现拖拽排序功能
2018/04/23 PHP
如何让动态插入的javascript脚本代码跑起来。
2007/01/09 Javascript
javascript Firefox与IE 替换节点的方法
2010/02/24 Javascript
JSQL 基于客户端的成绩统计实现方法
2010/05/05 Javascript
JqGrid web打印实现代码
2011/05/31 Javascript
动态获取复选框checkbox选中个数的jquery代码
2013/06/25 Javascript
jQuery控制TR显示隐藏的三种常用方法
2014/08/21 Javascript
javascript检测浏览器的缩放状态实现代码
2014/09/28 Javascript
AngularJS入门教程之路由与多视图详解
2016/08/19 Javascript
实例讲解DataTables固定表格宽度(设置横向滚动条)
2017/07/11 Javascript
在vue中使用G2图表的示例代码
2019/03/19 Javascript
小程序两种滚动公告栏的实现方法
2019/09/17 Javascript
原生js实现弹窗消息动画
2020/11/20 Javascript
[44:10]2018DOTA2亚洲邀请赛 4.5 淘汰赛 EG vs VP 第一场
2018/04/06 DOTA
[34:41]夜魇凡尔赛茶话会 第二期02:你画我猜
2021/03/11 DOTA
Python 返回汉字的汉语拼音
2009/02/27 Python
python格式化字符串实例总结
2014/09/28 Python
python中装饰器级连的使用方法示例
2017/09/29 Python
pyqt5简介及安装方法介绍
2018/01/31 Python
Python实现的视频播放器功能完整示例
2018/02/01 Python
tensorflow 获取模型所有参数总和数量的方法
2018/06/14 Python
python 实现提取log文件中的关键句子,并进行统计分析
2019/12/24 Python
python 实现IP子网计算
2021/02/18 Python
H5 canvas中width、height和style的宽高区别详解
2018/11/02 HTML / CSS
优秀毕业生自荐信范文
2014/01/01 职场文书
银行领导证婚词
2014/01/11 职场文书
社区工作者先进事迹
2014/01/18 职场文书
2014法院干警廉洁警示教育思想汇报
2014/09/13 职场文书
2014幼儿园中班工作总结
2014/11/10 职场文书
拾金不昧表扬稿
2015/01/16 职场文书
入党自传范文2015
2015/06/26 职场文书
婚宴领导致辞
2015/07/28 职场文书
一年级语文教学随笔
2015/08/14 职场文书
2015新员工工作总结范文
2015/10/15 职场文书
浅谈redis五大数据结构和使用场景
2021/04/12 Redis