首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OSError:在尝试使用ImageFont.py时无法打开资源

OSError:在尝试使用ImageFont.py时无法打开资源
EN

Stack Overflow用户
提问于 2016-12-29 14:57:12
回答 2查看 5.8K关注 0票数 3

我使用python生成令牌作为安全措施。以下是代码:

代码语言:javascript
复制
        from PIL import Image, ImageDraw, ImageFont

        image=Image.new("RGBA",(220,20),(255,255,255))
        image_base=ImageDraw.Draw(image)
        font = ImageFont.truetype("arial.ttf", 15)
        font.size = 16
        image_base.text((80,0),emailed_password,(0,0,0),font=font)
        image_bytes=BytesIO()
        image.save(image_bytes,format='png')
        logo_file = open('static\images\RCI_logo.jpg', 'rb')
        logo_bytes = logo_file.read()
        logo_file.close()
        token_name='registration_token'
        secret_code_attachment = MIMEImage(image_bytes.getvalue(), name = '{0}.png'.format(token_name))
        secret_code_attachment.add_header('Content-ID', '<{0}>'.format(token_name))
        secret_code_attachment.add_header('Content-Disposition', 'inline')
        message.attach(secret_code_attachment)
        logo_file_name='logo_file_svg'
        logo_attachment = MIMEImage(logo_bytes, name = '{0}.jpg'.format(logo_file_name))
        logo_attachment.add_header('Content-ID', '<{0}>'.format(logo_file_name))
        logo_attachment.add_header('Content-Disposition', 'inline')
        message.attach(logo_attachment)
        html = 'some message'
        html_attachment = MIMEText(html, 'html')
        message.attach(html_attachment)
        smtpObj.sendmail(sender,receiver,message.as_string())

错误似乎在于加载字体的能力。虽然我是从PIL导入ImageFont。

错误消息如下:

代码语言:javascript
复制
ERROR in app: Exception on /registration [POST], referer: http://my.com/registration

Traceback (most recent call last):, referer: http://my.com/registration

File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1982, in wsgi_app, referer: http://my.com.com/registration

response = self.full_dispatch_request(), referer:    http://my.com/registration

File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1614, in full_dispatch_request, referer: http://my.com/registration

rv = self.handle_user_exception(e), referer: http://my.com/registration

File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1517, in handle_user_exception, referer: http://my.com/registration

reraise(exc_type, exc_value, tb), referer: http://my.com/registration

File "/usr/local/lib/python3.5/dist-packages/flask/_compat.py", line 33, in reraise, referer: http://my.com/registration

raise value, referer: http://my.com/registration

File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1612, in full_dispatch_request, referer: http://my.com/registration

rv = self.dispatch_request(), referer: http://my.com/registration

File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1598, in dispatch_request, referer: http://my.com/registration

return self.view_functions[rule.endpoint](**req.view_args), referer: http://my.com/registration

File "/var/www/rci/rciApp/__init__.py", line 260, in registration, referer: http://my.com/registration

font = ImageFont.truetype("arial.ttf", 15), referer: http://my/registration

File "/usr/lib/python3/dist-packages/PIL/ImageFont.py", line 239, in truetype, referer: http://my.com/registration

return FreeTypeFont(font, size, index, encoding), referer: http://my.com/registration

File "/usr/lib/python3/dist-packages/PIL/ImageFont.py", line 128, in __init__, referer: http://my.com/registration

self.font = core.getfont(font, size, index, encoding), referer: http://my.com/registration

OSError: cannot open resource, referer: http://my.com/registration

我的理解是,ImageFont导致了这个问题,因为它无法加载合适的字体。我不知道怎么解决它。更令人费解的是,在我把它放到服务器上之前,它一直在工作。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-02-23 05:29:15

遇到同样的问题,很明显,font文件不能被PIL找到。

font = ImageFont.truetype("arial.ttf", 15)中,arial.ttf找不到。

解决方案是cd /usr/share/fonts/truetype,并找到一个可用的字体文件,然后用它替换"arial.ttf"

票数 3
EN

Stack Overflow用户

发布于 2022-04-10 04:54:39

调试打印您的文件路径并检查它是否正确。

我也遇到过同样的情况。其他静态资源正在加载,除了字体,即使字体存在于静态文件夹中--结果是路径上的愚蠢错误。

我打过:

代码语言:javascript
复制
ImageFont.truetype(STATIC_ROOT + "fonts/arial.ttf", 44)

而不是:

代码语言:javascript
复制
ImageFont.truetype(STATIC_ROOT + "/fonts/arial.ttf", 44)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41382116

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档