I have this code:
img = Image.new('L', (26, 17), color=255)
fnt = ImageFont.truetype('/assets/david.ttf', 23)
d = ImageDraw.Draw(img)
d.text((0, -1), "hi", font=fnt, fill=100)
img.save('newimg.png')
and it works just fine.
but what I'm trying to do, is to make a loop and in each iteration to use different font, if I change this line:
fnt = ImageFont.truetype('/assets/david.ttf', 23)
with this:
font_name = 'david.tff'
fnt = ImageFont.truetype('/assets/' + font_name, 23)
( even not in a loop )
I receive that error:
Traceback (most recent call last):
File ".../create_dataset.py", line 30, in <module>
fnt = ImageFont.truetype('/assets/' + font_name, 23)
File "...\venv\lib\site-packages\PIL\ImageFont.py", line 642, in truetype
return freetype(font)
File "...\venv\lib\site-packages\PIL\ImageFont.py", line 639, in freetype
return FreeTypeFont(font, size, index, encoding, layout_engine)
File "...\venv\lib\site-packages\PIL\ImageFont.py", line 188, in __init__
font, size, index, encoding, layout_engine=layout_engine
OSError: cannot open resource
Why using variable inside the ImageFont.truetype
makes this error and how can it be solved?
So it took me until now, but this error occurred not because of using a variable ( which is by itself looks weird since it can be used anywhere ), but because of my misspelling:
I used david.tff
instead of david.ttf
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.