OpenCV for Python 实战(一):获取图片拍摄GPS地址并自动添加水印

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工具,具体使用方法见百度地图的文档,咱们这里就直接提供源码了哈~

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()

cv2.putText()是OpenCV(开源计算机视觉库)中的一种方法,用于向图像或视频帧添加文本。它需要以下参数:

  • image:需要添加文本的输入图像或帧
  • text:要添加的文本字符串
  • org:一个表示文本起始坐标的元组(x,y)
  • fontFace:字体类型(可以从预定义的字体列表中选择)
  • fontScale:字体大小(乘以字体厚度的比例因子)
  • color:BGR格式的文本颜色(例如,绿色为(0,255,0))
  • thickness:用于绘制文本的线条粗细(可以为负数以获得填充文本)
  • lineType:用于绘制文本的线条类型(例如,cv2.LINE_AA表示反锯齿线)

该方法返回一个添加了文本的图像。

在这里插入图片描述

但是我们要注意的是,OpenCV只能显示英文,无法显示中文。所以我们使用施加魔法pip install opencv-python-rolling下载OpenCV v5.0版本

在这里插入图片描述
OK 轻松获得了咱们的图片
在这里插入图片描述

在这里插入图片描述

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()方法获得了添加水印后的图像。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
很好,下面我来为你介绍一下 OpenCV-Python 实现目标检测的过程。 OpenCV-Python 是一款基于 Python 的计算机视觉库,它可以帮助我们完成各种图像处理任务,包括图像的读取、处理、显示等。在使用它进行目标检测时,我们可以使用传统方法 HOG+SVM 实现。 HOG(Histogram of Oriented Gradient)特征是一种常用的图像特征,它可以描述图像中的边缘、角点等信息,其中梯度方向直方图是 HOG 特征的核心部分。在目标检测中,我们需要先提取出图像中的 HOG 特征,然后再使用 SVM(Support Vector Machine)进行分类,从而实现目标检测。 下面是一个简单的 OpenCV-Python 实现目标检测的示例代码: ```python import cv2 # 加载 SVM 分类器 svm = cv2.ml.SVM_load('svm.xml') # 加载测试图片 img = cv2.imread('test.jpg') # 创建 HOG 描述符 hog = cv2.HOGDescriptor() # 设置 SVM 分类器 hog.setSVMDetector(svm) # 检测目标并绘制矩形框 rects, weights = hog.detectMultiScale(img, winStride=(8, 8), padding=(32, 32), scale=1.05) for (x, y, w, h) in rects: cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2) # 显示结果 cv2.imshow('result', img) cv2.waitKey(0) cv2.destroyAllWindows() ``` 在代码中,我们首先加载了训练好的 SVM 分类器,并加载了测试图片。然后创建了 HOG 描述符,并设置 SVM 分类器。最后使用 detectMultiScale 函数检测目标,并绘制矩形框,最终在窗口中显示检测结果。 当然,这仅仅是一个简单的示例,实际的目标检测过程还需要根据具体的应用场景进行调整和优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

地摊主老袁

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

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

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

打赏作者

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

抵扣说明:

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

余额充值