图片添加文字
import cv2
img = cv2.imread('pyyy.png')
img2 = img.copy() # 备份操作
font = cv2.FONT_HERSHEY_SIMPLEX # 定义字体
imgzi = cv2.putText(img, '000', (50, 50), font, 1.2, (255, 255, 255), 2)
# 图像,文字内容, 坐标 ,字体,大小,颜色,字体厚度
cv2.imshow('origin',img) # 显示原始图像
cv2.waitKey()
cv2.imshow('putText',imgzi) # 显示添加文字操作的图像
cv2.waitKey()
cv2.imshow('backup',img2) # 显示原图像的备份
cv2.waitKey()
cv2.destroyAllWindows()
视频显示时间戳
import cv2
import datetime
cap = cv2.VideoCapture(0)
print(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
print(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
#cap.set(3, 3000)
#cap.set(4, 3000)
#print(cap.get(3))
#print(cap.get(4))
while(cap.isOpened()):
ret, frame = cap.read()
if ret == True:
font = cv2.FONT_HERSHEY_SIMPLEX
text = 'Width: '+ str(cap.get(3)) + ' Height:' + str(cap.get(4))
datet = str(datetime.datetime.now())
frame = cv2.putText(frame, text, (10, 50), font, 1,
(0, 255, 255), 2, cv2.LINE_AA)
frame = cv2.putText(frame, datet, (10, 100), font, 1,
(0, 255, 255), 2, cv2.LINE_AA)
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
cv2.destroyAllWindows()