可以使用OpenCV的cv2.putText()函数来实现。
示例代码:
import cv2
读取图片
img = cv2.imread('image.jpg')
定义文本框的位置
x, y = 10, 30
定义文本框的大小
width, height = 200, 50
定义文本框的背景颜色
bg_color = (0, 0, 255)
绘制文本框
cv2.rectangle(img, (x, y), (x + width, y + height), bg_color, -1)
定义文本
text = 'Hello World!'
在文本框内添加文字
cv2.putText(img, text, (x + 10, y + 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
保存图片
cv2.imwrite('result.jpg', img)