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 返回汉字的汉语拼音
Feb 27 Python
解决Django模板无法使用perms变量问题的方法
Sep 10 Python
python实现对csv文件的列的内容读取
Jul 04 Python
Django配置MySQL数据库的完整步骤
Sep 07 Python
Python帮你识破双11的套路
Nov 11 Python
python读取Kafka实例
Dec 23 Python
Python使用OpenPyXL处理Excel表格
Jul 02 Python
深入了解Python enumerate和zip
Jul 16 Python
Python执行时间的几种计算方法
Jul 31 Python
Python ConfigParser模块的使用示例
Oct 12 Python
Python爬虫分析微博热搜关键词的实现代码
Feb 22 Python
浅谈Python中的函数(def)及参数传递操作
May 25 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 版本]
2007/03/20 PHP
wordpress之wp-settings.php
2007/08/17 PHP
php自动给文章加关键词链接的函数代码
2012/11/29 PHP
基于php验证码函数的使用示例
2013/05/03 PHP
PHP类中的魔术方法(Magic Method)简明总结
2014/07/08 PHP
php生成不重复随机数、数组的4种方法分享
2015/03/30 PHP
php读取torrent种子文件内容的方法(测试可用)
2016/05/03 PHP
PHP中的use关键字及文件的加载详解
2016/11/28 PHP
PHP For循环字母A-Z当超过26个字母时输出AA,AB,AC
2020/02/16 PHP
URL编码转换,escape() encodeURI() encodeURIComponent()
2006/12/27 Javascript
一个JS翻页效果
2007/07/23 Javascript
javascript jscroll模拟html元素滚动条
2012/12/18 Javascript
jquery修改属性值实例代码(设置属性值)
2014/01/06 Javascript
使用原生js实现页面蒙灰(mask)效果示例代码
2014/06/20 Javascript
浅谈JSON和JSONP区别及jQuery的ajax jsonp的使用
2014/11/23 Javascript
JS上传图片前实现图片预览效果的方法
2015/03/02 Javascript
如何解决手机浏览器页面点击不跳转浏览器双击放大网页
2016/07/01 Javascript
微信小程序 教程之事件
2016/10/18 Javascript
详解Vue2中组件间通信的解决全方案
2017/07/28 Javascript
学习LayUI时自研的表单参数校验框架案例分析
2019/07/29 Javascript
微信sdk实现禁止微信分享(使用原生php实现)
2019/11/15 Javascript
vue 检测用户上传图片宽高的方法
2020/02/06 Javascript
[01:08:24]DOTA2-DPC中国联赛 正赛 RNG vs Phoenix BO3 第一场 2月5日
2021/03/11 DOTA
Django 限制用户访问频率的中间件的实现
2018/08/23 Python
python多进程使用及线程池的使用方法代码详解
2018/10/24 Python
Python中的正则表达式与JSON数据交换格式
2019/07/03 Python
一篇文章搞懂python的转义字符及用法
2020/09/03 Python
如何通过安装HomeBrew来安装Python3
2020/12/23 Python
开学典礼感言
2014/02/16 职场文书
《童年》教学反思
2014/02/18 职场文书
护理专业毕业生自我鉴定总结
2014/03/24 职场文书
活动总结报告怎么写
2014/07/03 职场文书
建筑专业毕业生求职信
2014/09/30 职场文书
2014年业务工作总结
2014/11/17 职场文书
SQL Server中常用截取字符串函数介绍
2022/03/16 SQL Server
九大龙王魂骨,山龙王留下躯干骨,榜首死的最憋屈(被捏碎)
2022/03/18 国漫