Hello 我们在OpenCV每天的基础博客当中已经更新了很多了,那么今天我们就来结合前几天的内容。做一个获取属性然后添加对应属性的水印。那让我们赶快开始吧~
可能很多人并没有听说过这个专有名词,这是一个专门储存相片的相关信息的地方。大家一般在操作的时候右键打开属性,点击“详细信息”就会出现这样的界面了~
今天咱们就来详细了解一下什么是EXIF啦~
EXIF是指Exchangeable Image File
Format(可交换图像文件格式)的缩写。它是一种存储在数字照片中的元数据格式,包含了照片的各种信息,如拍摄时间、相机型号、曝光参数、GPS坐标等。EXIF数据可以为摄影师、设计师、摄影爱好者等提供了有用的信息,同时也为照片的管理和编辑提供了便利。
EXIF数据通常被存储在JPEG、TIFF和RAW等图像文件格式中,通过使用相机或其他设备的内置工具或第三方软件,可以轻松查看和编辑这些数据。在数字摄影中,EXIF数据对于了解照片的拍摄环境和条件非常有用,例如确定曝光设置、焦距、ISO感光度等。
除了基本的拍摄信息外,EXIF数据还可以包含版权信息、作者、拍摄地点等更多信息。这使得照片的归档、共享和管理更加方便。然而,也需要注意保护个人隐私,因为可能包含有关拍摄者或拍摄地点的敏感信息。
而今天,咱们需要获取的就是这个GPS信息
实际上,我们有很多方法获取其中的信息。但是为了让我们的程序变得更加简单,我们在这里使用第三方库帮我们完成这个读取的操作。
安装代码: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工具,具体使用方法见百度地图的文档,咱们这里就直接提供源码了哈~
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)
cv2.putText()是OpenCV(开源计算机视觉库)中的一种方法,用于向图像或视频帧添加文本。它需要以下参数:
该方法返回一个添加了文本的图像。
但是我们要注意的是,OpenCV只能显示英文,无法显示中文。所以我们使用施加魔法pip install opencv-python-rolling
下载OpenCV v5.0版本
import exifread,requests,json,cv2
cv2.imwrite("水印添加.jpg",cv2.putText(img = cv2.imread("./LFS.jpg"),text = 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"],org = (400,400),fontFace = cv2.FONT_HERSHEY_SIMPLEX,thickness = 20,fontScale = 10,color = (255,255,255)))
正常长度代码
import exifread,requests,json,cv2
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"]
cv2.imwrite("水印添加.jpg",cv2.putText(img = cv2.imread("./LFS.jpg"),text = direction,org = (400,400),fontFace = cv2.FONT_HERSHEY_SIMPLEX,thickness = 20,fontScale = 10,color = (255,255,255)))
我们使用了EXIFread库获取了图片的经纬值,然后通过Requests访问百度地图的API获得了我们的实际地位,用json库转换之后,用最新版的OpenCV的putText()
方法获得了添加水印后的图像。