python学习记录三:关于ImageFont.truetype(‘Arial.ttf‘, 36)执行报错以及解决方案

news/2024/11/29 6:29:49/

代码如下:

from PIL import Image, ImageFilter, ImageFont, ImageDrawimport random# 随机字母
def rndChar():return chr(random.randint(65, 90))# 随机颜色1:
def rndColor():return random.randint(64, 255), random.randint(64, 255), random.randint(64, 255)# 随机颜色2:
def rndColor2():return random.randint(32, 127), random.randint(32, 127), random.randint(32, 127)# 240 x 60
width = 60 * 4
height = 60
image = Image.new('RGB', (width, height), (255, 255, 255))
# 创建Font对象
font = ImageFont.truetype('Arial.ttf', 36)
# 创建Draw对象
draw = ImageDraw.Draw(image)
# 填充每个像素
for x in range(width):for y in range(height):draw.point((x, y), fill=rndColor())
# 输出文字
for t in range(4):draw.text((60 * t + 10, 10), rndChar(), font=font, fill=rndColor2())
# 模糊:
image = image.filter(ImageFilter.BLUR)
image.save('code.jpg', 'jpeg')

执行报错:

"D:\Program Files\python-3.7.9\python.exe" D:/py/test-three/test_01.py
Traceback (most recent call last):File "D:/py/test-three/test_01.py", line 26, in <module>font = ImageFont.truetype('Arial.ttf', 36)File "D:\Program Files\python-3.7.9\lib\site-packages\PIL\ImageFont.py", line 844, in truetypereturn freetype(font)File "D:\Program Files\python-3.7.9\lib\site-packages\PIL\ImageFont.py", line 841, in freetypereturn FreeTypeFont(font, size, index, encoding, layout_engine)File "D:\Program Files\python-3.7.9\lib\site-packages\PIL\ImageFont.py", line 194, in __init__font, size, index, encoding, layout_engine=layout_engine
OSError: cannot open resource进程已结束,退出代码1

分析报错信息以及ImageFont.py中truetype源码,“:exception OSError: If the file could not be read.”,分析字体文件无法读取,可本地搜索字体,查看字体名称为“arial.ttf”,将“Arial.ttf”改为“arial.ttf”,执行成功,另一种解决方法就是获取字体绝对路径,如“C:\Windows\Fonts\Arial.ttf”,更换执行成功。


http://www.ppmy.cn/news/624475.html

相关文章

YOLOV5服务器部署启动时卡住:Downloading https://ultralytics.com/assets/Arial.ttf

问题&#xff1a; YOLOV5服务器部署启动时卡主&#xff0c;一直显示&#xff1a;Downloading https://ultralytics.com/assets/Arial.ttf 解决方法&#xff1a; 1、点击链接地址&#xff1a;https://ultralytics.com/assets/Arial.ttf&#xff0c;下载Arial.ttf字体包到本地。…

Downloading https://ultralytics.com/assets/Arial.ttf to ...... yolov5报错解决

yolov5模型用的是github上YOLOv5官方的, 在autodl服务器上训练时&#xff0c;Arial.ttf字体会自动下载&#xff0c;但是无法下载。&#xff08;当然在自己电脑上pycharm跑不会报这个错&#xff0c;可以下载&#xff09; 解决方法&#xff1a; class Annotator:if RANK in (-1,…

Downloading https://ultralytics.com/assets/Arial.ttf下载过于缓慢问题

用的yolov5官方源码https://github.com/ultralytics/yolov5 一直在这个界面无法下载 Downloading https://ultralytics.com/assets/Arial.ttf to C:\Users\\AppData\Roaming\Ultralytics\Arial.ttf... 最后干脆直接点击链接下载Arial.ttf,放到了后面的目录下面运行通过。

Downloading https://ultralytics.com/assets/Arial.ttf to /data/..../.config/Ultralytics/Arial.ttf

1.报错&#xff1a;缺少字体Arial.ttf 2.字体链接&#xff1a;https://ultralytics.com/assets/Arial.ttf 3.方法&#xff1a;下载该链接的字体&#xff0c;然后放到/data/..../.config/Ultralytics/该路径下&#xff0c;具体路径看自己的报错。 具体函数位置如下图&#xff1…

YOLOv报错Downloading https://ultralytics.com/assets/Arial.ttf to /root/.config/Ultralytics/Arial.ttf..

yolo在服务器上跑得时候报错Downloading https://ultralytics.com/assets/Arial.ttf to /root/.config/Ultralytics/Arial.ttf..&#xff0c; 建议将&#xff0c;general.py中得&#xff0c; FONT Arial.ttf # https://ultralytics.com/assets/Arial.ttf 此代码中&#xf…

Linux中matplotlib设置Times New Roman、Arial字体

linux安装字体&#xff1a; sudo apt install ttf-mscorefonts-installer sudo fc-cache 确认是否安装成功&#xff1a; fc-match Arial fc-match Times# 也可以通过下面代码查看系统安装的所有字体 fc-list 清理matplotlib缓存&#xff1a; cd ~/.cache/matplotlib rm * -r 在…

YOLOv报错(Arial.ttf)

YOLOv5需要用到这个字体进行标注&#xff0c;所以会自动下载&#xff0c;但是如果网络等原因&#xff0c;会导致下载错误。如下&#xff1a; D:\Programs\conda411\envs\envZhao\python.exe D:/coding/python3/yolov5-6.1/detect.py Downloading https://ultralytics.com/asse…

Arial

欢迎访问: www.ptcms.cnArial是一套随同多套微软应用软件所分发的无衬线体TrueType字型。Monotype设计这套字型是要以其作为一个较便宜的替代品与Linotype的Helvetica竞争。由于Helvetica是支援Adobe PostScript语言的打印机所指定的其中一款字型&#xff0c;用户选用它能确保文…