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:
- Oserror: cannot open resource (pilot error handling)
- [Solved] Runtime Warning: Font Glyph 20108 is missing. Font.set Fu text (s,0.0,flags=flags), RuntimeWarningGlyph20108missingfromcurrentfontfontsettexts00flagsflags
- Captcha reports an error java.lang.arrayindexoutofboundsexception
- [Solved] peewee error: ImportError: No module named ‘MySQLdb’;pymysql error: from . import connections # noqa: E402
- [Solved] SQLAlchemy Error: ImportError: cannot import name ‘TYPE_CHECKING’
- import win32api; Importerror: DLL load failed: the specified program was not found
- Matplotlib.pyplot Plug-in Chinses Font Error: RuntimeWarning: Glyph 26376 missing from current font
- [Solved] Ride.py startup error: Error in atexit._run_exitfuncs
- Python pandas.read_ Oserror: initializing from file failed
- Python: How to Read file initialization from file failed by panda