当我们用opencv在图片上写汉字,使用的是putText,会遇到只能写英文,写中文的话,会遇到报错。
这个时候我们只需要转成PIL格式,对其输入中文,再转会opencv格式就好
代码如下:
import cv2
import os
import numpy as np
from PIL import ImageFont, ImageDraw, Imagefps = 30
size = (1280, 720)
name = 1
# videowriter = cv2.VideoWriter("result.mp4",-1, fps, size)img = np.ones((720, 1280, 3), dtype=np.uint8)*255
txt = '这人间袅袅炊烟'#设置需要显示的字体
fontpath = "f" # 32为字体大小
font = uetype(fontpath, 32)
img_pil = Image.fromarray(img) #openc转PIL
draw = ImageDraw.Draw(img_pil)
((100, 350), txt, font = font, fill = (255, 0, 0))
img = np.array(img_pil) #PIL转opencv#显示图片
cv2.imshow("img",img)
cv2.waitKey(0)
然后是效果图
本文发布于:2024-02-04 06:27:28,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170701029153105.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |