PIL 往图片上写入中文报错 UnicodeEncodeError: ‘latin-1’ codec can’t encode characters in position
PIL 往图片上写入中文报错 UnicodeEncodeError: ‘latin-1’ codec can’t encode characters in position
s = str(config.paddle.label[labels[clsid]])
text2 = "{} {:.4f}".format(s, score)
# text = "{} {:.4f}".format(labels[clsid], score)
tw, th = draw.textsize(text2)
draw.rectangle(
[(xmin + 1, ymin - th), (xmin + tw + 1, ymin)], fill=color)
draw.text((xmin + 1, ymin - th), text2, fill=(255, 255, 255))
没有传入字体
s = str(config.paddle.label[labels[clsid]])
text2 = "{} {:.4f}".format(s, score)
# text = "{} {:.4f}".format(labels[clsid], score)
#声明中文字体,从网上下载
ttfont = ImageFont.truetype("./python/arialuni.ttf",15)
#传入 font=ttfont
tw, th = draw.textsize(text2,font=ttfont)
draw.rectangle(
[(xmin + 1, ymin - th), (xmin + tw + 1, ymin)], fill=color)
#传入 font=ttfont
draw.text((xmin + 1, ymin - th), text2,font=ttfont, fill=(255, 255, 255))