
# This is a sample Python script.
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
import cv2
import numpy
from PIL import Image, ImageDraw, ImageFont
def paint_chinese_opencv(img, text, left, top, textColor=(0, 255, 0), textSize=20):
if (isinstance(img, numpy.ndarray)): # 判断是否OpenCV图片类型
img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
# 创建一个可以在给定图像上绘图的对象
draw = ImageDraw.Draw(img)
# 字体的格式
fontStyle = ImageFont.truetype(
"font/simsun.ttc", textSize, encoding="utf-8")
# 绘制文本
draw.text((left, top), text, textColor, font=fontStyle)
# 转换回OpenCV格式
return cv2.cvtColor(numpy.asarray(img), cv2.COLOR_RGB2BGR)
def print_hi(name):
# Use a breakpoint in the code line below to debug your script.
print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint.
def view():
# image = cv2.imread("test.png")
image = paint_chinese_opencv(cv2.imread("test.png"), "opencv 中文处理", left=100, top=20, textColor=(0, 0, 0), textSize=48)
cv2.imshow("show", image)
# 显示10秒
cv2.waitKey(10000)
cv2.destoryAllWindows()
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
# print_hi('PyCharm')
view()