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

Unexpected segmentation fault in ImageFont (cpy3.9, pillow8.0.1, Windows10 1909) #5033

Closed
GalaxySnail opened this issue Nov 6, 2020 · 5 comments
Labels

Comments

@GalaxySnail
Copy link

What did you do?

just run the following code:

import pathlib
from PIL import ImageFont, ImageDraw, Image

def img_generate(text):
    img = Image.new('RGB', (40, 100), (255, 255, 255))
    # https://github.com/adobe-fonts/source-code-pro/raw/release/TTF/SourceCodePro-Light.ttf
    fontpath = pathlib.Path(__file__).parent.absolute().joinpath("SourceCodePro-Light.ttf")
    font = ImageFont.truetype(str(fontpath), 64)
    draw = ImageDraw.Draw(img)
    draw.text((0, 0), text, font = font, fill = (0,0,0,0))
    return img

img_generate("A")
print("success")

What did you expect to happen?

Python should not crash here.

What actually happened?

The situation is randomly one of these three: succeed, print "segment fault" only, print full traceback.

success
zsh: segmentation fault  python test.py
Traceback (most recent call last):
  File "D:\Documents\Programing\python\hackergame2020\test.py", line 14, in <module>
    img_generate("A")
  File "D:\Documents\Programing\python\hackergame2020\test.py", line 11, in img_generate
    draw.text((0, 0), text, font = font, fill = (0,0,0,0))
  File "C:\Users\galaxysnail\AppData\Roaming\Python\Python39\site-packages\PIL\ImageDraw.py", line 379, in text
    draw_text(ink)
  File "C:\Users\galaxysnail\AppData\Roaming\Python\Python39\site-packages\PIL\ImageDraw.py", line 324, in draw_text
    mask, offset = font.getmask2(
  File "C:\Users\galaxysnail\AppData\Roaming\Python\Python39\site-packages\PIL\ImageFont.py", line 651, in getmask2
    size, offset = self.font.getsize(
OSError: invalid face handle
zsh: segmentation fault  python test.py

What are your OS, Python and Pillow versions?

  • OS: Windows10 1909, 18363.1139, X86_64
  • Python: CPython 3.9.0 (tags/v3.9.0:9cf6752, Oct 5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)] on win32
  • Pillow: Pillow 8.0.1 from pypi
@nulano
Copy link
Contributor

nulano commented Nov 7, 2020

Do you have Raqm installed?

You can check this by running python -m PIL; if you have it installed you will see the following, or similar, near the top of the output:

--- RAQM (Bidirectional Text) support ok, loaded 0.7.0

If you do have Raqm installed, can you try the following change, and see if that fixes the crash?

    font = ImageFont.truetype(str(fontpath), 64, layout_engine=ImageFont.LAYOUT_BASIC)

@GalaxySnail
Copy link
Author

Do you have Raqm installed?

You can check this by running python -m PIL; if you have it installed you will see the following, or similar, near the top of the output:

--- RAQM (Bidirectional Text) support ok, loaded 0.7.0

If you do have Raqm installed, can you try the following change, and see if that fixes the crash?

    font = ImageFont.truetype(str(fontpath), 64, layout_engine=ImageFont.LAYOUT_BASIC)

Thanks, it seems to be working.

And I wonder what caused that? Can it be fixed?

@nulano
Copy link
Contributor

nulano commented Nov 7, 2020

The default layout_engine depends on whether Raqm is installed, using it if available, and falling back on basic layout if not. In this case your build of Raqm seems to be incompatible, but this is detected too late, only when actually rendering text.

I think this is a duplicate of #3066, which I believe is caused by a mismatch in the FreeType version used by Pillow and that used by Raqm. The solution for Windows should be to update Raqm (or rather update the build of Raqm to one using a newer version of FreeType, since the Raqm version is the same). Alternatively, you could just uninstall Raqm and use basic layout.

@GalaxySnail
Copy link
Author

Thanks a lot! I found it. I'm using msys2, and mingw-w64-x86_64-libraqm is installed as a dependency, and msys2/mingw64/bin is added in my system PATH env. Now I removed msys2/mingw64/bin from system PATH.

@nulano
Copy link
Contributor

nulano commented Nov 7, 2020

Ah, that's not quite the same; the MSYS2 version of Raqm links against the MSYS2 version of FreeType which is compiled with GCC and therefore incompatible with the MSVC build of FreeType used in Pillow wheels. In general, it is probably not a good idea to have MSYS2 binaries in the system path.

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

No branches or pull requests

3 participants