OpenCV - 在图片上写上中文

from PIL import Image,ImageDraw,ImageFont
import numpy as np
import cv2

def drop_text_for_cvimage(image, test, font, font_color=(0,0,255), font_point=(0, 0)):
    pil_image = Image.fromarray(cv2.cvtColor(image,cv2.COLOR_BGR2RGB))
    draw = ImageDraw.Draw(pil_image)
    draw.text(font_point, test, font_color, font)
    image = cv2.cvtColor(np.array(pil_image),cv2.COLOR_RGB2BGR)
    return image


image = np.zeros((100, 100, 3)).astype("uint8")
font = ImageFont.truetype('./NotoSansCJK-Black.ttc', 20, encoding="utf-8")
image = drop_text_for_cvimage(image, "hello", font, font_color=(255,0,0), font_point=(0,0))
cv2.imwrite("./image.png", image)

在这里插入图片描述

你可能感兴趣的:(#,OpenCV)