Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OSError when opening a font file whose name is not in English #3480

Closed
jdhao opened this issue Nov 28, 2018 · 3 comments
Closed

OSError when opening a font file whose name is not in English #3480

jdhao opened this issue Nov 28, 2018 · 3 comments

Comments

@jdhao
Copy link

jdhao commented Nov 28, 2018

Issue description

When I tried to open a font using ImageFont.truetype() method (the font file name is in Chinese):

from PIL import ImageFont
font = ImageFont.truetype("方正黄草简体.ttf")

I encountered an error:

> D:\Anaconda\lib\site-packages\PIL\ImageFont.py in truetype(font, size, index, encoding, layout_engine)
>     278
>     279     try:
> --> 280         return FreeTypeFont(font, size, index, encoding, layout_engine)
>     281     except IOError:
>     282         ttf_filename = os.path.basename(font)
> 
> D:\Anaconda\lib\site-packages\PIL\ImageFont.py in __init__(self, font, size, index, encoding, layout_engine)
>     143         if isPath(font):
>     144             self.font = core.getfont(font, size, index, encoding,
> --> 145                                      layout_engine=layout_engine)
>     146         else:
>     147             self.font_bytes = font.read()
> 
> OSError: cannot open resource

When I change the font name to English name, for example, fzhcjt.tff, I can open the same font file without any error.

What are your OS, Python and Pillow versions?

  • OS: Windows 10 Pro 1803
  • Python: 3.6.5
  • Pillow: 5.3.0

I have also tested on my CentOS machine and this issue does not exist.

@radarhere radarhere changed the title OSError when open a font file whose name is not in English OSError when opening a font file whose name is not in English Nov 28, 2018
@hugovk
Copy link
Member

hugovk commented Nov 28, 2018

I get something different (Pillow 5.3.0, macOS High Sierra) for both Chinese and English characters:

Python 3.7.1 (default, Nov  6 2018, 18:45:35)
[Clang 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from PIL import ImageFont
>>> font = ImageFont("方正黄草简体.ttf")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable
>>> font = ImageFont("arial.ttf")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable

Instead, use ImageFont.truetype(font_path, font_size):

>>> ImageFont.truetype("arial.ttf", 15)
<PIL.ImageFont.FreeTypeFont object at 0x1096e5f60>
>>> ImageFont.truetype("方正黄草简体.ttf", 15)
<PIL.ImageFont.FreeTypeFont object at 0x1096ea080>
>>>
>>> font = ImageFont.truetype("方正黄草简体.ttf", 15)
>>>
>>> from PIL import Image, ImageDraw
>>> im = Image.new("RGB", (200, 100), "white")
>>> draw = ImageDraw.Draw(im)
>>> draw.text((10, 10), "abc", font=font, fill="black")
>>> im.show()

Produces:

image

See https://pillow.readthedocs.io/en/5.3.x/reference/ImageFont.html

Do you get the same OSError with ImageFont.truetype(font_path, font_size)?

@jdhao
Copy link
Author

jdhao commented Nov 28, 2018

@hugovk , sorry, I have pasted the wrong code. The code to produce has been corrected. I am getting this error using ImageFont.truetype().

image

@radarhere
Copy link
Member

This looks like a duplicate of #3145

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants