使用pillow生成图片验证码发生OSError

mac2024-01-31  43

使用pillow生成图片验证码发生OSError的解决方法

这是我遇到的OSError: 解析: 本错误是由于django 无法找到指定的字体而引发的错误:

def create_validate_code(size=(120, 30), chars=init_chars, img_type="GIF", mode="RGB", bg_color=(255, 255, 255), fg_color=(0, 0, 255), font_size=18, font_type="Arial.ttf", #这里指定字体 length=4, draw_lines=True, n_line=(1, 2), draw_points=True, point_chance = 2): 错误就在font_type这里。django会自动到 C:\Windows\Fonts\文件夹里找指定的字体, 但是我的电脑无法查找到Arial.ttf字体,我的程序需要给出绝对路径才能解决错误。

解决方法: 把font_type 改为 font_type = r" C:\Windows\Fonts\Arial.ttf" 解决问题生成如下验证码:

最新回复(0)