可以使用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)