图片上打印中文

    python中利用cv2.putText函数无法输出中文,可以借助pil模块实现该功能。

import cv2
import PIL.Image as Image
import PIL.ImageDraw as ImageDraw
import PIL.ImageFont as ImageFont
import numpy as np
 
#构建图像并用cv2.putText输出
img = np.zeros((100, 300), np.uint8)
txt_loc = (10, 20)
txt = u'中国China'
cv2.putText(img, txt.encode('utf-8'), txt_loc, 1, 1, (255, 255, 255))
 
#转换为pil文件格式,并打印字符。注意彩色图像的通道顺序:cv2为bgr, pil为rgb
pil_im = Image.fromarray(img)
draw = ImageDraw.Draw(pil_im)
txt_loc = (10, 60)
ttf_path = 'C:\\Users\\Administrator\\Desktop\\ipython notebook\\FZLTXHJW.TTF' #字库文件
font = ImageFont.truetype(ttf_path, 20, encoding="utf-8")
font10 = font.font_variant(size=10, encoding='utf-8') #修改字体大小
draw.text(txt_loc, txt, (255),font=font) #两通道图像时,color为(255);三通道图像是,color为(255, 0, 0),表示红色 
cv2.imshow('show_pil',np.array(pil_im))
cv2.waitKey(0)

输出结果如下。ps: cv2.putText的编码格式为utf-8,pil模块的编码格式为unicode。

  • 保存中文名称的图片
    dst_path = 'G:\\Data\\plate\\识别\\京A12345.jpg'
    cv2.imencode('.jpg', img_cv)[1].tofile(dst_path)  # 保存

     

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值