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中执行系统命令的方法示例详解
Sep 14 Python
Pycharm设置界面全黑的方法
May 23 Python
Python实现的微信好友数据分析功能示例
Jun 21 Python
python3 flask实现文件上传功能
Mar 20 Python
python用opencv批量截取图像指定区域的方法
Jan 24 Python
python使用递归的方式建立二叉树
Jul 03 Python
Django的Modelforms用法简介
Jul 27 Python
Django models.py应用实现过程详解
Jul 29 Python
Python自动采集微信联系人的实现示例
Feb 28 Python
使用python无账号无限制获取企查查信息的实例代码
Apr 17 Python
pandas按照列的值排序(某一列或者多列)
Dec 13 Python
python 视频下载神器(you-get)的具体使用
Jan 06 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连接mysql数据库代码
2009/03/10 PHP
php面向对象全攻略 (九)访问类型
2009/09/30 PHP
兼容性最强的PHP生成缩略图的函数代码(修改版)
2011/01/18 PHP
分享一个超好用的php header下载函数
2014/01/31 PHP
解密ThinkPHP3.1.2版本之模板继承
2014/06/19 PHP
php隐藏实际地址的文件下载方法
2015/04/18 PHP
php发送html格式文本邮件的方法
2015/06/10 PHP
php框架CodeIgniter主从数据库配置方法分析
2018/05/25 PHP
javascript实现动态侧边栏代码
2014/02/19 Javascript
jQuery实现鼠标划过修改样式的方法
2015/04/14 Javascript
jQuery实现进度条效果代码
2015/12/17 Javascript
React组件生命周期详解
2017/07/03 Javascript
深入浅出es6模板字符串
2017/08/26 Javascript
JS中关于正则的巧妙操作
2017/08/31 Javascript
JS基于封装函数实现的表格分页完整示例
2018/06/26 Javascript
用Node提供静态文件服务的方法
2018/07/06 Javascript
vue地址栏直接输入路由无效问题的解决
2018/11/15 Javascript
element-ui tooltip修改背景颜色和箭头颜色的实现
2019/12/16 Javascript
Vue+ElementUI 中级联选择器Bug问题的解决
2020/07/31 Javascript
在Python中的Django框架中进行字符串翻译
2015/07/27 Python
Python selenium 自动化脚本打包成一个exe文件(推荐)
2020/01/14 Python
Tensorflow实现多GPU并行方式
2020/02/03 Python
Python3.7在anaconda里面使用IDLE编译器的步骤详解
2020/04/29 Python
Python内置异常类型全面汇总
2020/05/28 Python
Python ConfigParser模块的使用示例
2020/10/12 Python
matplotlib运行时配置(Runtime Configuration,rc)参数rcParams解析
2021/01/05 Python
LTD Commodities:礼品,独特发现,家居装饰,家用器皿
2017/08/11 全球购物
高级电工工作职责
2013/11/21 职场文书
毕业生自我鉴定
2013/12/04 职场文书
老总助理工作岗位职责
2014/02/06 职场文书
党员批评与自我批评
2014/02/12 职场文书
政治表现评语
2014/05/04 职场文书
继续教育个人总结
2015/03/03 职场文书
市场督导岗位职责
2015/04/10 职场文书
Spring Boot 底层原理基础深度解析
2022/04/03 Java/Android
微信小程序实现轮播图指示器
2022/06/25 Javascript