captcha库报错"OSError: cannot open resource"

2018/04/24 17:42
阅读数 92

问题描述

在win平台上python虚拟环境下使用captcha库生成验证码报错OSError: cannot open resource

代码

from captcha.image import ImageCaptcha
from PIL import Image, ImageFont
text='1234'
image=ImageCaptcha()
captcha=image.generate_image(text)
captcha_image=Image.open(captcha)
captcha_image.show()

报错

E:\Code\计算机视觉大作业\venv\Scripts\python.exe E:/Code/计算机视觉大作业/code/temp.py
Traceback (most recent call last):
  File "E:/Code/计算机视觉大作业/code/temp.py", line 8, in <module>
    captcha=image.generate_image(text)
  File "E:\Code\计算机视觉大作业\venv\lib\site-packages\captcha\image.py", line 226, in generate_image
    im = self.create_captcha_image(chars, color, background)
  File "E:\Code\计算机视觉大作业\venv\lib\site-packages\captcha\image.py", line 197, in create_captcha_image
    images.append(_draw_character(c))
  File "E:\Code\计算机视觉大作业\venv\lib\site-packages\captcha\image.py", line 164, in _draw_character
    font = random.choice(self.truefonts)
  File "E:\Code\计算机视觉大作业\venv\lib\site-packages\captcha\image.py", line 122, in truefonts
    for n in self._fonts
  File "E:\Code\计算机视觉大作业\venv\lib\site-packages\captcha\image.py", line 123, in <listcomp>
    for s in self._font_sizes
  File "E:\Code\计算机视觉大作业\venv\lib\site-packages\PIL\ImageFont.py", line 261, in truetype
    return FreeTypeFont(font, size, index, encoding, layout_engine)
  File "E:\Code\计算机视觉大作业\venv\lib\site-packages\PIL\ImageFont.py", line 144, in __init__
    self.font = core.getfont(font, size, index, encoding, layout_engine=layout_engine)
OSError: cannot open resource

Process finished with exit code 1

解决

在试过网上各种方案都不行后发现是因为虚拟环境的路径中包含了中文,换成不带中文的路径即可。

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部