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库转换之后,输出了咱们图片的拍摄地址。