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 list中append()与extend()用法分享
Mar 24 Python
跟老齐学Python之使用Python操作数据库(1)
Nov 25 Python
Python机器学习之决策树算法
Dec 22 Python
django中静态文件配置static的方法
May 20 Python
python 3.6.4 安装配置方法图文教程
Sep 18 Python
对python中矩阵相加函数sum()的使用详解
Jan 28 Python
python实现Excel文件转换为TXT文件
Apr 28 Python
python实现比对美团接口返回数据和本地mongo数据是否一致示例
Aug 09 Python
Python队列、进程间通信、线程案例
Oct 25 Python
python实现程序重启和系统重启方式
Apr 16 Python
tensorflow模型转ncnn的操作方式
May 25 Python
Python中相见恨晚的技巧
Apr 13 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
isset和empty的区别
2007/01/15 PHP
用mysql内存表来代替php session的类
2009/02/01 PHP
学习php笔记 字符串处理
2010/10/19 PHP
php通过session防url攻击方法
2014/12/10 PHP
分享PHP计算两个日期相差天数的代码
2015/12/23 PHP
PHP面向对象中new self()与 new static()的区别浅析
2017/08/17 PHP
PHP日期和时间函数的使用示例详解
2020/08/06 PHP
juqery 学习之三 选择器 简单 内容
2010/11/25 Javascript
一个简单的动态加载js和css的jquery代码
2014/09/01 Javascript
javascript实现信息增删改查的方法
2015/07/25 Javascript
解决angular的$http.post()提交数据时后台接收不到参数值问题的方法
2015/12/10 Javascript
canvas实现钟表效果
2017/02/13 Javascript
JavaScript纯色二维码变成彩色二维码
2020/07/23 Javascript
微信小程序页面间值传递的两种方法
2018/11/26 Javascript
微信小程序map组件结合高德地图API实现wx.chooseLocation功能示例
2019/01/23 Javascript
详解Nodejs get获取远程服务器接口数据
2019/03/26 NodeJs
axios如何利用promise无痛刷新token的实现方法
2019/08/27 Javascript
angular inputNumber指令输入框只能输入数字的实现
2019/12/03 Javascript
python中模块查找的原理与方法详解
2017/08/11 Python
python的paramiko模块实现远程控制和传输示例
2017/10/13 Python
python os用法总结
2018/06/08 Python
浅谈Pandas:Series和DataFrame间的算术元素
2018/12/22 Python
对PyQt5中树结构的实现方法详解
2019/06/17 Python
python pygame实现球球大作战
2019/11/25 Python
tensorflow 实现打印pb模型的所有节点
2020/01/23 Python
Python实现疫情通定时自动填写功能(附代码)
2020/05/27 Python
python连接mysql数据库并读取数据的实现
2020/09/25 Python
创先争优活动方案
2014/02/12 职场文书
物业工程部经理岗位职责
2015/04/09 职场文书
稽核岗位职责范本
2015/04/13 职场文书
2015年机关纠风工作总结
2015/05/15 职场文书
社区安全温馨提示语
2015/07/14 职场文书
2015年教务主任工作总结
2015/07/22 职场文书
Spring中bean的生命周期之getSingleton方法
2021/06/30 Java/Android
「我的青春恋爱物语果然有问题。-妄言录-」第20卷封面公开
2022/03/21 日漫
三星 3nm 芯片将于第二季度开始量产
2022/04/29 数码科技