如何用python获取到照片拍摄时的详细位置(附源码)
作者:上进小菜猪 发布时间:2023-04-22 05:04:18
标签:python,照片,位置
一.引言
先看获取到的效果
拍摄时间:2021:12:18 16:22:13
照片拍摄地址:('内蒙古自治区包头市昆都仑区', '内蒙古自治区', '包头市', '昆都仑区', '多米幼儿园东南360米')
我们的女朋友给我们发来一张照片我们如何获取到她的位置呢?
用手机拍照会带着GPS信息,原来没注意过这个,因此查看下并使用代码获取照片里的GPS信息
查看图片文件属性
1.读取照片信息,获取坐标
ExifRead
Python library to extract EXIF data from tiff and jpeg files.
安装
pip install exifread
读取GPS
import exifread
import re
def read():
GPS = {}
date = ''
f = open("C:\\Users\\24190\\Desktop\\小朱学长.jpg",'rb')
contents = exifread.process_file(f)
for key in contents:
if key == "GPS GPSLongitude":
print("经度 =", contents[key],contents['GPS GPSLatitudeRef'])
elif key =="GPS GPSLatitude":
print("纬度 =",contents[key],contents['GPS GPSLongitudeRef'])
#print(contents)
read()
运行
我们得到了一个简易的gps地址
如果想要读取全部的拍摄信息:
# 读取照片的GPS经纬度信息
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}
2.通过baidu Map的API将GPS信息转换成地址。
众所周知gps和百度的经纬度会有误差,那么我们需要调用百度转换接口,这个百度目前没有开源。
# 通过baidu Map的API将GPS信息转换成地址。
def find_address_from_GPS(GPS):
"""
使用Geocoding API把经纬度坐标转换为结构化地址。
:param GPS:
:return:
"""
secret_k ey = 'XXX'
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]
print(content)
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"]
location = baidu_map_address["result"]["sematic_description"]
return formatted_address, province, city, district, location
然后在主函数输出:
二.源码附上!!!
# coding=utf-8
import exifread
import re
import json
import requests
import os
# 转换经纬度格式
def latitude_and_longitude_convert_to_decimal_system(*arg):
"""
经纬度转为小数, param arg:
:return: 十进制小数
"""
return float(arg[0]) + ((float(arg[1]) + (float(arg[2].split('/')[0]) / float(arg[2].split('/')[-1]) / 60)) / 60)
# 读取照片的GPS经纬度信息
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}
# 通过baidu Map的API将GPS信息转换成地址。
def find_address_from_GPS(GPS):
"""
使用Geocoding API把经纬度坐标转换为结构化地址。
:param GPS:
:return:
"""
secret_ke y = 'zbLsuDDL4CS2U0M4KezOZZbGUY9iWtVf'
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]
print(content)
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"]
location = baidu_map_address["result"]["sematic_description"]
return formatted_address, province, city, district, location
if __name__ == '__main__':
GPS_info = find_GPS_image(pic_path='小朱学长.jpg')
address = find_address_from_GPS(GPS=GPS_info)
print("拍摄时间:" + GPS_info.get("date_information"))
print('照片拍摄地址:' + str(address))
注意事项
1.照片的地址信息等,一般的手机相机默认是打开的。
2.微信和QQ里面发送原图,信息都会完整的保留下来。
3.代码里面需要处理在照片我放到了代码的同文件夹下,所以没有写路径,大家可以自己写路径,或者放到于代码相同的路径下即可。
来源:https://blog.csdn.net/weixin_52908342/article/details/122012234
0
投稿
猜你喜欢
- 在Python中可以通过在属性变量名前加上双下划线定义属性为私有属性,如例子:#! encoding=UTF-8 class A:
- 我们做WEB程序开发过程中,经常会为了表格Table中的cellSpacing,cellpadding等样式的设置发愁,经常没有办法可想了,
- 字体设计是人类商业活动的需求,它随着时代和科学技术的进步而不断地变化着。被广泛应用于网络生活的各个方面。现代字体设计在计算机技术的应用中已经
- 虽然Golang的GC自打一开始,就被人所诟病,但是经过这么多年的发展,Golang的GC已经改善了非常多,变得非常优秀了。以下是Golan
- 页面中无法看见页面,指向的连接网页无法显示 解决方法:1、首先在Dreamweaver中不能中文作为文件名。连目录名也最好是英文的。2、如果
- 个人认为python的paramiko模块是运维人员必学模块之一,其ssh登录功能是旅行居家必备工具。安装paramiko很简单,pip i
- 前言之前的一篇文章已经讲过怎样获取链接,怎样获得参数了,详情请看python爬取京东商城普通篇,本文将详细介绍利用python爬虫框架scr
- 【OpenCV】⚠️高手勿入! 半小时学会基本操作⚠️图像梯度概述OpenCV 是一个跨平台的计算机视觉库, 支持多语言, 功能强大. 今天
- Python 是一种高级的,动态的,多泛型的编程语言。Python代码很多时候看起来就像是伪代码一样,因此你可以使用很少的几行可读性很高的代
- python3.7 打包成exe程序环境:pycharm2018.1+win7+python3.7工具:pyinstaller1、安装pyi
- 本文实例为大家分享了基于wxPython的GUI实现输入对话框的具体代码,供大家参考,具体内容如下编程时,免不了要输入一些参数等,这时输入对
- 之前已经写过一篇关于Django外键的文章,但是当时并没有介绍如何根据外键对数据的操作,也就是如何通过主表查询子表或者通过子表查询主表的信息
- 年初的时候收藏过一篇关于mysqlreport的报表解读,和内置的show status,和show variables相比mysqlrep
- 一、从外部文档中粘贴时,如果只要文字而不想要其格式,可以使用“Edit→paste as text”命令,不要直接用Ctrl+V。二、当有浏
- Updates(2019.8.14 19:53)吃饭前用这个方法实战了一下,吃完回来一看好像不太行:跑完一组参数之后,到跑下一组参数时好像没
- 本文实例讲述了Python栈算法的实现与简单应用。分享给大家供大家参考,具体如下:原理:栈作为一种数据结构,是一种只能在一端进行插入和删除操
- 背景图像领域内的一个国内会议快要召开了,要发各种邀请邮件,之后要录入、统计邮件回复(参会还是不参会等)。如此重要的任务,老师就托付给我了。p
- 作为WIMP(Window/Icon/Menu/Pointing Device)界面设计的关键部分,图标在人机交互设计中无所不在。随着人们对
- 1 什么是注释注释就是对代码的解释和说明,其目的是让人们能够更加轻松地了解代码。注释是编写程序时,写程序的人给一个语句、程序段、函数等的解释
- 本文实例讲述了Python单例模式的使用方法。分享给大家供大家参考。具体如下:方法一import threading