需要安装的包
pip install pillow
如何将字体的内容画出来
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
def get_draw_image(wolf, font_list, filename):
# 创建图片
image = Image.new("RGB", (1800, 1000), color=(255, 255, 255))
# 准备一支笔
img_draw = ImageDraw.Draw(image)
# 给出画图的字体
img_font = ImageFont.truetype(wolf, size=40)
line_length = 40
# 准备文字
new_line = []
for i in range(len(font_list)):
uni = font_list[i]
uni = uni.encode().decode("unicode-escape")
if i % line_length == 0 and i != 0:
# 该换换了,写入该行
new_line_s = "".join(new_line)
# 可以画到图片了
img_draw.text((20, (i // line_length + 1) * line_length), new_line_s, fill=1, font=img_font)
new_line = [uni]
else:
# 正常该行的内容
new_line.append(uni)
if new_line:
new_line_s = "".join(new_line)
# 可以画到图片了
img_draw.text((20, (len(font_list) // line_length + 2) * line_length), new_line_s, fill=1, font=img_font)
# 完成上述操作. 你只是在内存中画了一张图.
# 保存到硬盘上
image.save(filename+".jpg")
文字识别
``
文字识别
APP_ID = 'xxxxxxxx'
API_KEY = 'xxxxxxxxxxxxxxxx'
SECRET_KEY = 'xxxxxxxxxx'
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
f_shopnum = open("shopnum.jpg", "rb")
r_shopnum = client.basicGeneral(f_shopnum.read())
print(r_shopnum)
说明
# 直接运行报 报ModuleNotFoundError: No module named 'chardet',查阅资料发信啊in需要安装pip install chardet
# 再次运行发现又给出一个警告:
# RequestsDependencyWarning: urllib3 (1.26.9) or chardet (5.0.0)/charset_normalizer (2.0.12)
# doesn't match a supported version!
# 经过查阅相关资料,其处理方法为协助冲突的urllib3和chardet模块,然后重新安装并升级requests模块,
# 之后问题即可得到解决,其卸载和安装命令如下
# pip uninstall urllib3 chardet
#
# pip install --upgrade requests
# 再次安装 pip install chardet
标签:img,length,字体,关于,new,操作,line,font,chardet
From: https://www.cnblogs.com/pdxt666/p/16800271.html