【有趣的Python小程序】EXIFread+Requests 获取图片拍摄地址

Hello 我们在OpenCV每天的基础博客当中已经更新了很多了,那么今天我们就来结合前几天的内容,做一个获取属性的程序。那让我们赶快开始吧~

图片EXIF

可能很多人并没有听说过这个专有名词,这是一个专门储存相片的相关信息的地方。大家一般在操作的时候右键打开属性,点击“详细信息”就会出现这样的界面了~
在这里插入图片描述

今天咱们就来详细了解一下什么是EXIF啦~

EXIF是指Exchangeable Image File

Format(可交换图像文件格式)的缩写。它是一种存储在数字照片中的元数据格式,包含了照片的各种信息,如拍摄时间、相机型号、曝光参数、GPS坐标等。EXIF数据可以为摄影师、设计师、摄影爱好者等提供了有用的信息,同时也为照片的管理和编辑提供了便利。

EXIF数据通常被存储在JPEG、TIFF和RAW等图像文件格式中,通过使用相机或其他设备的内置工具或第三方软件,可以轻松查看和编辑这些数据。在数字摄影中,EXIF数据对于了解照片的拍摄环境和条件非常有用,例如确定曝光设置、焦距、ISO感光度等。

除了基本的拍摄信息外,EXIF数据还可以包含版权信息、作者、拍摄地点等更多信息。这使得照片的归档、共享和管理更加方便。然而,也需要注意保护个人隐私,因为可能包含有关拍摄者或拍摄地点的敏感信息。

而今天,咱们需要获取的就是这个GPS信息

在这里插入图片描述

Python 获取EXIF

exifread库

实际上,我们有很多方法获取其中的信息。但是为了让我们的程序变得更加简单,我们在这里使用第三方库帮我们完成这个读取的操作。

安装代码pip install exifread

在这里插入图片描述

使用方法

我们只需要使用process_file()这个函数就可以直接获取属性

import exifread

tag = exifread.process_file(open("./LFS.jpg","rb"))
print(tag)

在这里插入图片描述

现在我们来获取一下['GPS GPSLatitude']这是咱们的纬度信息。我们可以看到这是一个EXIFread库当中定义的数据类型。
在这里插入图片描述
我们使用printable将其转换成str数据

在这里插入图片描述

再用split(",")分割成列表类型方便读取

在这里插入图片描述

然后咱们先获取度的数据,用replace()代替多余的“[”

import exifread

latitude = int(exifread.process_file(open("./LFS.jpg","rb"))['GPS GPSLatitude'].printable.split(",")[0].replace("[","")) + int(exifread.process_file(open("./LFS.jpg","rb"))['GPS GPSLatitude'].printable.split(",")[1])/60
longitude = int(exifread.process_file(open("./LFS.jpg","rb"))['GPS GPSLongitude'].printable.split(",")[0].replace("[","")) + int(exifread.process_file(open("./LFS.jpg","rb"))['GPS GPSLongitude'].printable.split(",")[1])/60
print(longitude,latitude)

同理咱们编写代码,咱们也就成功地获取了经纬值
在这里插入图片描述

转换成文字地址

在这里我们选择百度地图的web工具,咱们访问一下百度地图的开发者中心,注册完成后,咱们点击应用管理
在这里插入图片描述

将我们的ak码放入下面的程序当中

在这里插入图片描述

import exifread,requests,json

latitude = int(exifread.process_file(open("./LFS.jpg","rb"))['GPS GPSLatitude'].printable.split(",")[0].replace("[","")) + int(exifread.process_file(open("./LFS.jpg","rb"))['GPS GPSLatitude'].printable.split(",")[1])/60
longitude = int(exifread.process_file(open("./LFS.jpg","rb"))['GPS GPSLongitude'].printable.split(",")[0].replace("[","")) + int(exifread.process_file(open("./LFS.jpg","rb"))['GPS GPSLongitude'].printable.split(",")[1])/60
message = json.loads(requests.get("https://api.map.baidu.com/reverse_geocoding/v3/?ak=你的ak&output=json&coordtype=wgs84ll&location="+str(latitude)+","+str(longitude)).text)
direction = message["result"]["addressComponent"]["province"] + message["result"]["addressComponent"]["city"] + message["result"]["addressComponent"]["district"] + message["result"]["addressComponent"]["town"]
print(direction)

在这里插入图片描述

代码简化

import exifread,requests,json
print(json.loads(requests.get("https://api.map.baidu.com/reverse_geocoding/v3/?ak=你的ak&output=json&coordtype=wgs84ll&location="+str(int(exifread.process_file(open("./LFS.jpg","rb"))['GPS GPSLatitude'].printable.split(",")[0].replace("[","")) + int(exifread.process_file(open("./LFS.jpg","rb"))['GPS GPSLatitude'].printable.split(",")[1])/60)+","+str(int(exifread.process_file(open("./LFS.jpg","rb"))['GPS GPSLongitude'].printable.split(",")[0].replace("[","")) + int(exifread.process_file(open("./LFS.jpg","rb"))['GPS GPSLongitude'].printable.split(",")[1])/60)).text)["result"]["addressComponent"]["province"] + json.loads(requests.get("https://api.map.baidu.com/reverse_geocoding/v3/?ak=你的ak&output=json&coordtype=wgs84ll&location="+str(int(exifread.process_file(open("./LFS.jpg","rb"))['GPS GPSLatitude'].printable.split(",")[0].replace("[","")) + int(exifread.process_file(open("./LFS.jpg","rb"))['GPS GPSLatitude'].printable.split(",")[1])/60)+","+str(int(exifread.process_file(open("./LFS.jpg","rb"))['GPS GPSLongitude'].printable.split(",")[0].replace("[","")) + int(exifread.process_file(open("./LFS.jpg","rb"))['GPS GPSLongitude'].printable.split(",")[1])/60)).text)["result"]["addressComponent"]["city"] + json.loads(requests.get("https://api.map.baidu.com/reverse_geocoding/v3/?ak=你的ak&output=json&coordtype=wgs84ll&location="+str(int(exifread.process_file(open("./LFS.jpg","rb"))['GPS GPSLatitude'].printable.split(",")[0].replace("[","")) + int(exifread.process_file(open("./LFS.jpg","rb"))['GPS GPSLatitude'].printable.split(",")[1])/60)+","+str(int(exifread.process_file(open("./LFS.jpg","rb"))['GPS GPSLongitude'].printable.split(",")[0].replace("[","")) + int(exifread.process_file(open("./LFS.jpg","rb"))['GPS GPSLongitude'].printable.split(",")[1])/60)).text)["result"]["addressComponent"]["district"] + json.loads(requests.get("https://api.map.baidu.com/reverse_geocoding/v3/?ak=你的ak&output=json&coordtype=wgs84ll&location="+str(int(exifread.process_file(open("./LFS.jpg","rb"))['GPS GPSLatitude'].printable.split(",")[0].replace("[","")) + int(exifread.process_file(open("./LFS.jpg","rb"))['GPS GPSLatitude'].printable.split(",")[1])/60)+","+str(int(exifread.process_file(open("./LFS.jpg","rb"))['GPS GPSLongitude'].printable.split(",")[0].replace("[","")) + int(exifread.process_file(open("./LFS.jpg","rb"))['GPS GPSLongitude'].printable.split(",")[1])/60)).text)["result"]["addressComponent"]["town"])

每日总结

我们使用了EXIFread库获取了图片的经纬值,然后通过Requests访问百度地图的API获得了我们的实际地位,用json库转换之后,输出了咱们图片的拍摄地址。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

地摊主老袁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值