Opencv+PIL显示中文并画框

import cv2
from PIL import Image, ImageDraw, ImageFont
import numpy as np
 
cap = cv2.VideoCapture(data/1.mp4)
while cap.isOpened():
    ret, frame = cap.read()
    # 显示中文字体并画框
    image = Image.fromarray(frame)
    draw = ImageDraw.Draw(image)  # 图片上打印
    x0, y0, x1, y1 = [50, 50, 300, 300]  # (x0,y0)左上,(x1,y1)右下
    draw.rectangle([x0, y0, x1, y1], outline=(0, 0, 255), width=4)  # 画框
    font = ImageFont.truetype("./simhei.ttf", 20, encoding="utf-8")
    draw.text((0, 0), "显示中文", (255, 0, 0), font=font)  # 参数1:打印坐标,参数2:文本,参数3:字体颜色,参数4:字体
    # image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
 
    # 显示视频
    cv2.imshow(frame, np.array(image))
    if cv2.waitKey(25) & 0xFF == ord(q):
        break
 
cap.release()
cv2.destroyAllWindows()
 

分享图片

相关文章
相关标签/搜索