Tag Archives: captcha

[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

Captcha reports an error java.lang.arrayindexoutofboundsexception

*Captcha reports an error java.lang.arrayindexoutofboundsexception

The following error message was found when starting the program, which has been tangled for a long time

Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
		at sun.font.CompositeFont.getSlotFont(CompositeFont.java:375)
		at sun.font.CompositeGlyphMapper.initMapper(CompositeGlyphMapper.java:81)
		at sun.font.CompositeGlyphMapper.<init>(CompositeGlyphMapper.java:62)
		at sun.font.CompositeFont.getMapper(CompositeFont.java:433)
		at sun.font.CompositeFont.canDisplay(CompositeFont.java:459)
		at java.awt.Font.canDisplay(Font.java:1980)
		at com.octo.captcha.component.image.fontgenerator.RandomFontGenerator.checkFontCanDisplayCharacters(RandomFontGenerator.java:207)
		at com.octo.captcha.component.image.fontgenerator.RandomFontGenerator.cleanFontList(RandomFontGenerator.java:163)
		at com.octo.captcha.component.image.fontgenerator.RandomFontGenerator.initializeFonts(RandomFontGenerator.java:79)
		at com.octo.captcha.component.image.fontgenerator.RandomFontGenerator.<init>(RandomFontGenerator.java:75)
		at lac.framework.captcha.ImageCaptchaServiceBean.afterPropertiesSet(ImageCaptchaServiceBean.java:74)
		at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
		at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
		... 35 more

Later, I checked some data and found that the deployed system did not use the font when using the verification code component
install the corresponding font and fix it. Fonts can refer to another documented article: installing fonts on Linux