好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

python 图像上写中文字体

本人处理图像的时候经常使用opencv的包,但是 cv2.putText 显示不了中文,所以查找了如何在python在图像上写中文的方法,在伟大的Stack Overflow上面找到一个方法,分享给大家。

本文的 Stack Overflow 网址: https://stackoverflow.com/questions/50854235/how-to-draw-chinese-text-on-the-image-using-cv2-puttextcorrectly-pythonopen

import cv2

from PIL import ImageFont, ImageDraw, Image

image = cv2.imread(‘image‘)

# 定义宋体路径 fontpath = ‘simsun.ttc‘

# 创建字体对象,并且指定字体大小

font = ImageFont.truetype(fontpath, 28)

# 把array格式转换成PIL的image格式

img_pil = Image.fromarray(image)

# 创建一个可用来对其进行draw的对象

draw = ImageDraw.Draw(img_pil)

# 在图像上写上你要写的子

draw.text((30, 60),  ‘你好,中国’, font=font, fill=(255, 255, 255))

# 把PIL的image格式转换回成array格式

save_image = np.array(img_pil)

cv2.imshow(‘image‘, image)

cv2.waitKey()

cv2.destroyAllWindows()

这样就可以在图像上看到中文了。

这边有个细节要提醒下大家,因为本人电脑系统是ubuntu18.04,所以下载的simsun.ttc打开文件一直有错误,所以就在github上找了一个

查看更多关于python 图像上写中文字体的详细内容...

  阅读:20次

上一篇: django-URL认识

下一篇:python 常见模块