Reputation: 820
I am using python to generate token as security measure. Here is the code:
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())
The error seems to be in ability to load font. Although I am importing ImageFont from PIL.
The error message is below:
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
My understanding is that ImageFont is causing the problem because it cannot load proper font. I am not sure how to fix it. Even more puzzling is that it used to work until I put it on the server.
Upvotes: 4
Views: 9728
Reputation: 5483
Debug print your file path and check that it is correct.
I encountered the same. Other static resources were loading except fonts even though the fonts were present in static folder — turns out it was silly mistake in path.
I had typed:
ImageFont.truetype(STATIC_ROOT + "fonts/arial.ttf", 44)
instead of:
ImageFont.truetype(STATIC_ROOT + "/fonts/arial.ttf", 44)
Upvotes: 0
Reputation: 3253
Meet same problem, It is obviously font
file cannot be found by PIL
.
In font = ImageFont.truetype("arial.ttf", 15)
, arial.ttf
cannot found.
The solution is cd /usr/share/fonts/truetype
and find a available font file then replace "arial.ttf"
with it.(For windows user, font files in C:\WINDOWS\Fonts
)
Upvotes: 5