[Solved] Captcha Library Error: “OSError: cannot open resource”

Problem description

Generate verification code using captcha library on WIN platform in Python virtual environment and report error oserror: cannot open resource

Code

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()

Error reporting

E:\Code\111\venv\Scripts\python.exe E:/Code/111/code/temp.py
Traceback (most recent call last):
  File "E:/Code/111/code/temp.py", line 8, in <module>
    captcha=image.generate_image(text)
  File "E:\Code\111\venv\lib\site-packages\captcha\image.py", line 226, in generate_image
    im = self.create_captcha_image(chars, color, background)
  File "E:\Code\111\venv\lib\site-packages\captcha\image.py", line 197, in create_captcha_image
    images.append(_draw_character(c))
  File "E:\Code\111\venv\lib\site-packages\captcha\image.py", line 164, in _draw_character
    font = random.choice(self.truefonts)
  File "E:\Code\111\venv\lib\site-packages\captcha\image.py", line 122, in truefonts
    for n in self._fonts
  File "E:\Code\111\venv\lib\site-packages\captcha\image.py", line 123, in <listcomp>
    for s in self._font_sizes
  File "E:\Code\111\venv\lib\site-packages\PIL\ImageFont.py", line 261, in truetype
    return FreeTypeFont(font, size, index, encoding, layout_engine)
  File "E:\Code\111\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

 

Settle

After trying various schemes on the Internet, I found that it is because the path of the virtual environment contains Chinese. Replace it with a path without Chinese

Similar Posts: