如何在 cv2.imshow() 创建的 window 上显示系统的当前时间

how to display current time of the system on window created by cv2.imshow()

我正在尝试在由 cv2.imshow() 创建的 window 上显示系统的当前时间,其中包含一些属性。我搜索了互联网,但找不到我想要的东西。我只想在 window 上查看当前时间。也许可以通过在 window 属性 中设置某种标志或 属性 来实现,但我无法在任何地方找到它。

在 window 上显示时间涉及两件事:

  1. 从系统获取时间
  2. 将时间显示为文本

以下 Python 代码说明了这两个步骤:

import cv2

#--- to obtain time ---
from time import strftime

#--- read the image ---
x = r'C:\Users\Jackson\Desktop\Stack\Homography\Spartacus.jpg'
img = cv2.imread(x, 1)
    
#--- Position the time at (10, 70) coordinate with certain font style, size and color ---
cv2.putText(img, strftime("%H:%M:%S"), (10,70), cv2.FONT_HERSHEY_SIMPLEX, 2,(0,255,0),2,cv2.LINE_AA)
cv2.imshow('img', img)

cv2.waitKey(0)
cv2.destroyAllWindows()

结果

上面的代码只显示时间。但是,如果要显示日期,请将以下内容打印为文本:

strftime("%Y-%m-%d")

编辑:

这是为了响应背景中的浅色。您可以使用 cv2.rectangle() 来获得任何颜色的矩形区域。尺寸、颜色、粗细、是否需要填充都由您决定。

下面将绘制一个尺寸为 (510 x 128) 且位置为 x = 384 且 y = 50 的填充白色矩形:

cv2.rectangle(img, (384, 50), (510, 128), (255, 255, 255), -1)

注意:一定要先画好矩形,然后再放上日期,确保日期在矩形区域的上方。