chineseText.py

这篇文章介绍了如何使用Python的OpenCV库将文本添加到图像中,通过cv2ImgAddText函数实现,适合初学者了解图像处理基本操作。
#coding=utf-8
#中文乱码处理

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



def cv2ImgAddText(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)
    fontText = ImageFont.truetype(
        "font/simsun.ttc", textSize, encoding="utf-8")
    draw.text((left, top), text, textColor, font=fontText)
    return cv2.cvtColor(numpy.asarray(img), cv2.COLOR_RGB2BGR)

D:/0125py/text3.py:116: UserWarning: Glyph 21015 (\N{CJK UNIFIED IDEOGRAPH-5217}) missing from current font. self.canvas.draw() D:/0125py/text3.py:116: UserWarning: Glyph 36710 (\N{CJK UNIFIED IDEOGRAPH-8F66}) missing from current font. self.canvas.draw() D:/0125py/text3.py:116: UserWarning: Glyph 25509 (\N{CJK UNIFIED IDEOGRAPH-63A5}) missing from current font. self.canvas.draw() D:/0125py/text3.py:116: UserWarning: Glyph 32452 (\N{CJK UNIFIED IDEOGRAPH-7EC4}) missing from current font. self.canvas.draw() D:/0125py/text3.py:116: UserWarning: Glyph 32455 (\N{CJK UNIFIED IDEOGRAPH-7EC7}) missing from current font. self.canvas.draw() D:/0125py/text3.py:116: UserWarning: Glyph 26102 (\N{CJK UNIFIED IDEOGRAPH-65F6}) missing from current font. self.canvas.draw() D:/0125py/text3.py:116: UserWarning: Glyph 38388 (\N{CJK UNIFIED IDEOGRAPH-95F4}) missing from current font. self.canvas.draw() D:/0125py/text3.py:116: UserWarning: Glyph 37319 (\N{CJK UNIFIED IDEOGRAPH-91C7}) missing from current font. self.canvas.draw() D:/0125py/text3.py:116: UserWarning: Glyph 38598 (\N{CJK UNIFIED IDEOGRAPH-96C6}) missing from current font. self.canvas.draw() D:/0125py/text3.py:116: UserWarning: Glyph 25240 (\N{CJK UNIFIED IDEOGRAPH-6298}) missing from current font. self.canvas.draw() D:/0125py/text3.py:116: UserWarning: Glyph 32447 (\N{CJK UNIFIED IDEOGRAPH-7EBF}) missing from current font. self.canvas.draw() D:/0125py/text3.py:116: UserWarning: Glyph 22270 (\N{CJK UNIFIED IDEOGRAPH-56FE}) missing from current font. self.canvas.draw() D:/0125py/text3.py:116: UserWarning: Glyph 65288 (\N{FULLWIDTH LEFT PARENTHESIS}) missing from current font. self.canvas.draw() D:/0125py/text3.py:116: UserWarning: Glyph 20998 (\N{CJK UNIFIED IDEOGRAPH-5206}) missing from current font. self.canvas.draw() D:/0125py/text3.py:116: UserWarning: Glyph 38047 (\N{CJK UNIFIED IDEOGRAPH-949F}) missing from current font. self.canvas.draw() D:/0125py/text3.py:116: UserWarning: Glyph 65289 (\N{FULLWIDTH RIGHT PARENTHESIS}) missing from current font. self.canvas.draw() D:/0125py/text3.py:116: UserWarning: Glyph 27969 (\N{CJK UNIFIED IDEOGRAPH-6D41}) missing from current font. self.canvas.draw() D:/0125py/text3.py:116: UserWarning: Glyph 31243 (\N{CJK UNIFIED IDEOGRAPH-7A0B}) missing from current font. self.canvas.draw() D:/0125py/text3.py:116: UserWarning: Glyph 24179 (\N{CJK UNIFIED IDEOGRAPH-5E73}) missing from current font. self.canvas.draw() D:/0125py/text3.py:116: UserWarning: Glyph 22343 (\N{CJK UNIFIED IDEOGRAPH-5747}) missing from current font. self.canvas.draw() D:/0125py/text3.py:186: UserWarning: Glyph 21015 (\N{CJK UNIFIED IDEOGRAPH-5217}) missing from current font. dialog.exec() D:/0125py/text3.py:186: UserWarning: Glyph 36710 (\N{CJK UNIFIED IDEOGRAPH-8F66}) missing from current font. dialog.exec() D:/0125py/text3.py:186: UserWarning: Glyph 25509 (\N{CJK UNIFIED IDEOGRAPH-63A5}) missing from current font. dialog.exec() D:/0125py/text3.py:186: UserWarning: Glyph 32452 (\N{CJK UNIFIED IDEOGRAPH-7EC4}) missing from current font. dialog.exec() D:/0125py/text3.py:186: UserWarning: Glyph 32455 (\N{CJK UNIFIED IDEOGRAPH-7EC7}) missing from current font. dialog.exec() D:/0125py/text3.py:186: UserWarning: Glyph 26102 (\N{CJK UNIFIED IDEOGRAPH-65F6}) missing from current font. dialog.exec() D:/0125py/text3.py:186: UserWarning: Glyph 38388 (\N{CJK UNIFIED IDEOGRAPH-95F4}) missing from current font. dialog.exec() D:/0125py/text3.py:186: UserWarning: Glyph 37319 (\N{CJK UNIFIED IDEOGRAPH-91C7}) missing from current font. dialog.exec() D:/0125py/text3.py:186: UserWarning: Glyph 38598 (\N{CJK UNIFIED IDEOGRAPH-96C6}) missing from current font. dialog.exec() D:/0125py/text3.py:186: UserWarning: Glyph 25240 (\N{CJK UNIFIED IDEOGRAPH-6298}) missing from current font. dialog.exec() D:/0125py/text3.py:186: UserWarning: Glyph 32447 (\N{CJK UNIFIED IDEOGRAPH-7EBF}) missing from current font. dialog.exec() D:/0125py/text3.py:186: UserWarning: Glyph 22270 (\N{CJK UNIFIED IDEOGRAPH-56FE}) missing from current font. dialog.exec() D:/0125py/text3.py:186: UserWarning: Glyph 65288 (\N{FULLWIDTH LEFT PARENTHESIS}) missing from current font. dialog.exec() D:/0125py/text3.py:186: UserWarning: Glyph 20998 (\N{CJK UNIFIED IDEOGRAPH-5206}) missing from current font. dialog.exec() D:/0125py/text3.py:186: UserWarning: Glyph 38047 (\N{CJK UNIFIED IDEOGRAPH-949F}) missing from current font. dialog.exec() D:/0125py/text3.py:186: UserWarning: Glyph 65289 (\N{FULLWIDTH RIGHT PARENTHESIS}) missing from current font. dialog.exec() D:/0125py/text3.py:186: UserWarning: Glyph 27969 (\N{CJK UNIFIED IDEOGRAPH-6D41}) missing from current font. dialog.exec() D:/0125py/text3.py:186: UserWarning: Glyph 31243 (\N{CJK UNIFIED IDEOGRAPH-7A0B}) missing from current font. dialog.exec() D:/0125py/text3.py:186: UserWarning: Glyph 24179 (\N{CJK UNIFIED IDEOGRAPH-5E73}) missing from current font. dialog.exec() D:/0125py/text3.py:186: UserWarning: Glyph 22343 (\N{CJK UNIFIED IDEOGRAPH-5747}) missing from current font. dialog.exec()
最新发布
08-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值