Using OpenCV to Add Chinese Characters in An Image
作者:XD / 发表: 2022年2月21日 23:59 / 更新: 2022年2月21日 23:59 / 编程笔记 / 阅读量:1965
Using OpenCV to Add Chinese Characters in An Image
# coding=UTF-8
import cv2
import numpy
from PIL import Image, ImageDraw, ImageFont
def cv2ImgAddText(img, text, left, top, textColor=(255, 0, 0), textSize=20):
if (isinstance(img, numpy.ndarray)):
img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
draw = ImageDraw.Draw(img)
fontText = ImageFont.truetype("simfang.ttf", textSize, encoding="utf-8") # you need to change the font path
draw.text((left, top), text, textColor, font=fontText)
return cv2.cvtColor(numpy.asarray(img), cv2.COLOR_RGB2BGR)
img = cv2.imread("test.jpg")
img = cv2ImgAddText(img, "大家好", 140, 60, (255, 0, 0), 20)
cv2.imwrite("test_result.jpg", img)
相关标签