Python在PIL和matplotlib图片上输出中文

编程入门 行业动态 更新时间:2024-10-27 01:36:17

Python在PIL和matplotlib图片上输出<a href=https://www.elefans.com/category/jswz/34/1769975.html style=中文"/>

Python在PIL和matplotlib图片上输出中文

一、matplotlib

import matplotlib.pyplot as plt
plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']
img = plt.imread("src_img/1698739560094.jpeg")
# plt.text(x, y, str, fontdict=None, withdash=False, **kwargs)
# x,y:位置(position)
# str:该position需要展示的值
# fontdict:字体
# withdash:宽度
plt.text(100,100,"我是")
plt.imshow(img)
plt.show()

二、PIL

import cv2
from PIL import Image, ImageDraw, ImageFontimg = cv2.imread("src_img/1698739560094.jpeg")
img_c = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
draw = ImageDraw.Draw(img_c)
font = ImageFont.truetype("simhei.ttf", 20)  # 设置字体 (参数字体,文字大小)
draw.text((100, 200), "哈哈哈", (255, 0, 0), font=font)  # 参数(字的位置,字的内容,颜色,字体)
img_c.show()

更多推荐

Python在PIL和matplotlib图片上输出中文

本文发布于:2023-11-15 02:21:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1592042.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中文   图片   Python   PIL   matplotlib

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!