如何使用cv2将文本放在图像上?



我试图在使用cv2的时间之前添加文本。有没有可能不仅加上时间?如果有,我该怎么做?

from utils import *
import cv2
import numpy as np
from datetime import datetime, timedelta
def get_black_background():
return np.zeros((500, 500))

start_time = datetime.strptime("2021-01-01", "%Y-%m-%d")
end_time = start_time + timedelta(days=1)

def generate_image_with_text(text):
image = get_black_background()
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(image, text, (int(image.shape[0]*0.35), int(image.shape[1]*0.5)), font, 1.5, (255, 255, 0), 2,
cv2.LINE_AA)
return image

while start_time < end_time:
text = convert_time_to_string(start_time)
image = generate_image_with_text(text)
cv2.imwrite(f"time_images/{text}.jpg", image)
start_time += timedelta(minutes=1)

如果你改变了"text"在generate_image_with_text中,它将打印它。您可以使用其他参数调用putText,例如用于"上"行(如果您的意思是"上"行)。时间字符串?):

beforeTime = "Before Caption![![enter image description here][1]][1]"
upperLineX =  (int(image.shape[0]*0.25) #Adjust multipliers etc. 
upperLineY = (int(image.shape[1]*[![enter image description here][1]][1]0.35)) 
cv2.putText(image, text, (int(image.shape[0]*0.35), int(image.shape[1]*0.5)), font, 1.5, (255, 255, 0), 2, cv2.LINE_AA)

要么把它添加到函数中,要么把它放在需要的地方。

如果你是指"before"及时,然后检查时间,无论你需要什么,如果<等,调用这样的替代文本等>

另外,你创建画布的代码使一个灰度(一个通道)和所有是白色或灰色;应该是:

def get_black_background():
return np.zeros((500, 500, 3)) #3 channels in order to show colors

最新更新