Pillow: OSError: invalid face handle

Created on 2 Apr 2018  ·  43Comments  ·  Source: python-pillow/Pillow

What did you do?

I ran the following code:

from PIL import Image, ImageDraw, ImageFont, features

print(features.check('raqm'))
print(Image.PILLOW_VERSION)

img = Image.new('RGB', (800, 300), color = (73, 109, 137))
d = ImageDraw.Draw(img)
fnt = ImageFont.truetype('NotoSansTibetan-Regular.ttf', 24, layout_engine=ImageFont.LAYOUT_BASIC)
d.text((10, 10), "༄༅། །སྒྲུབ།", font=fnt, fill=(255, 255, 0))
img.save('result.png')
fnt = ImageFont.truetype('NotoSansTibetan-Regular.ttf', 24, layout_engine=ImageFont.LAYOUT_RAQM)
print("things work until...")
res = fnt.getmask("༄༅། །སྒྲུབ།", features=['ccmp', 'abvs', 'blws', 'calt', 'liga', 'kern', 'abvm', 'blwm', 'mkmk'])

What did you expect to happen?

No segfault

What actually happened?

Here's the console output:

True
5.0.0
things work until...
Traceback (most recent call last):
  File "testmarche.py", line 13, in <module>
    res = fnt.getmask("༄༅། །སྒྲུབ།", features=['ccmp', 'abvs', 'blws', 'calt', 'liga', 'kern', 'abvm', 'blwm', 'mkmk'])
  File "/usr/local/lib/python3.5/dist-packages/PIL/ImageFont.py", line 163, in getmask
    return self.getmask2(text, mode, direction=direction, features=features)[0]
  File "/usr/local/lib/python3.5/dist-packages/PIL/ImageFont.py", line 166, in getmask2
    size, offset = self.font.getsize(text, direction, features)
OSError: invalid face handle
Erreur de segmentation

What versions of Pillow and Python are you using?

Bug Fonts Platform

Most helpful comment

what helped me was adding parameter layout_engine=ImageFont.LAYOUT_BASIC to .text function call.

Crashing:

fnt = ImageFont.truetype('../fonts/georgiai.ttf', 36)

Not Crashing:

fnt = ImageFont.truetype('../fonts/georgiai.ttf', 36, layout_engine=ImageFont.LAYOUT_BASIC)

All 43 comments

What platform are you on?

Linux, up-to-date Debian stable

It's happening OMM, OSX 10.12.6, from binary osx wheels of 5.1.0

============================================= FAILURES ==============================================
_________________________ TestImageFont_RaqmLayout.test_font_with_filelike __________________________

self = <test_imagefont.TestImageFont_RaqmLayout testMethod=test_font_with_filelike>

    def test_font_with_filelike(self):
        ImageFont.truetype(self._font_as_bytes(), FONT_SIZE,
                           layout_engine=self.LAYOUT_ENGINE)
>       self._render(self._font_as_bytes())

Tests/test_imagefont.py:109: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
Tests/test_imagefont.py:123: in _render
    ttf.getsize(txt)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <PIL.ImageFont.FreeTypeFont object at 0x10fd88090>, text = 'Hello World!', direction = None
features = None

    def getsize(self, text, direction=None, features=None):
>       size, offset = self.font.getsize(text, direction, features)
E       IOError: invalid face handle

../vpy27-test/lib/python2.7/site-packages/PIL/ImageFont.py:157: IOError
1 failed, 874 passed, 41 skipped in 50.48 seconds

@khaledhosny any hint on where it could come from? Is there a "safer" commit/release of raqm that should be used?

I don’t see where Raqm is involved here, invalid face handle is an error from FreeType, probably somewhere is passing an invalid FT_Face to Raqm.

I see... thanks for taking a look!

Closing as FreeType error. Can re-open if there's something to do inside Pillow.

What was the test procedure that made you think this is not a bug in pillow? Pillow could very well create a freetype handler and corrupt it, please elaborate on why you closed the issue

did anyone find a solution, for me this error started happening as soon in installed depends/install_raqm.sh

Also having this issue. It's also inconsistent. Sometimes my script completes with no error and sometimes throws this OSError (seemingly a random string in the array of strings I am drawing each time) then crashes.

Just started having this issue

Minimal code to reproduce:

from PIL import ImageFont

font = ImageFont.truetype("arial.ttf") # using arial as example, use any font you'd like
print(font.getsize("Hello World"))

Having the same issue. It started after I installed libraqm from https://github.com/HOST-Oman/libraqm .
Anyone knows how to uninstall libraqm, so that I can get rid of this issue.

@csoni111 If you built from Git master, run make uninstall.

Thanks @hugovk !
Removing libraqm resolved the issue for me.

getting error running with raqm how to resolve

code:

from PIL import Image, ImageDraw, ImageFont

img = Image.new('RGB', (800, 300), color = (73, 109, 137))
d = ImageDraw.Draw(img)
fnt = ImageFont.truetype('NotoSansTibetan-Regular.ttf', 24, layout_engine=ImageFont.LAYOUT_RAQM)
d.text((10, 10), "༄༅། །སྒྲུབ།", font=fnt, fill=(255, 255, 0))
img.save('result.png')

error

OSError                                   Traceback (most recent call last)
<ipython-input-1-b49c34b3a535> in <module>()
      4 d = ImageDraw.Draw(img)
      5 fnt = ImageFont.truetype('NotoSansTibetan-Regular.ttf', 24, layout_engine=ImageFont.LAYOUT_RAQM)
----> 6 d.text((10, 10), "༄༅། །སྒྲུབ།", font=fnt, fill=(255, 255, 0))
      7 img.save('result.png')

/usr/local/lib/python3.5/dist-packages/PIL/ImageDraw.py in text(self, xy, text, fill, font, anchor, *args, **kwargs)
    273             try:
    274                 mask, offset = font.getmask2(text, self.fontmode,
--> 275                                              *args, **kwargs)
    276                 xy = xy[0] + offset[0], xy[1] + offset[1]
    277             except AttributeError:

/usr/local/lib/python3.5/dist-packages/PIL/ImageFont.py in getmask2(self, text, mode, fill, direction, features, language, *args, **kwargs)
    183     def getmask2(self, text, mode="", fill=Image.core.fill, direction=None,
    184                  features=None, language=None, *args, **kwargs):
--> 185         size, offset = self.font.getsize(text, direction, features, language)
    186         im = fill("L", size, 0)
    187         self.font.render(text, im.id, mode == "1", direction, features, language)

OSError: invalid face handle

Hi, I have the same issue, it seems that downloading libraqm on Ubuntu 18.04 changes the Pillow installation, and cause this issue with non latin fonts. Even when removing it, the issue persists, and I have not been able to generate text without having it raise the exception.

When doing the same thing with a clean OS install, the error does not happen until I reinstall libraqm at which point I am back in a broken state.

Is there a known fix for this?

@Belval since you've been dedicated enough to try this on a clean OS, could you provide step-by-step instructions on how to replicate?

are there people around who can't reproduce the issue?

I can't replicate on my macOS Mojave, or on Ubuntu 18.04 on Travis CI.

@radarhere would a Dockerfile work? I can put one together today if that helps.

That would be great, thanks.

I would have done that a year ago if anyone told me it wasn't easily reproducible...

@Belval since you've been dedicated enough to try this on a clean OS, could you provide step-by-step instructions on how to replicate?
These steps i followed :
git clone https://github.com/HOST-Oman/libraqm.git
cd libraqm
sudo apt-get install libfreetype6-dev libharfbuzz-dev libfribidi-dev libglib2.0-dev gtk-doc-tools -y
./autogen.sh
./configure
make
make install
git clone https://github.com/python-pillow/Pillow.git
sudo apt-get install libtiff5-dev libjpeg8-dev zlib1g-dev \ libfreetype6-dev liblcms2-dev libwebp-dev libharfbuzz-dev libfribidi-dev \ tcl8.6-dev tk8.6-dev python-tk -y
MAX_CONCURRENCY=1 python setup.py build_ext --enable-freetype install

Even normal installation i am getting same error :
./install_raqm.sh
pip install Pillow

Here's a complete demo in a Vagrant vm:

https://github.com/eroux/pillow-bug

Believe it or not, running the vagrant on my macOS machine works fine.

I created a docker-images branch to test buster, and it works fine.

@chaitusvk what operating system are you using?

how so very frustrating... did you run the part with vagrant ssh, cd /vagrant/file/, etc. described in the README ? curiously if I just run the test at the end of the provisioning it doesn't show any error. What else could I do to debut this thing? There's a segfault so maybe a stacktrace would be helpful?

here's one:
core.zip

tell me if there's anything else I can do

I can't read the file inside the zip for some reason.

Unless I can reproduce the problem, I think that someone else might have to solve this one. That said, I've created a branch with number of print statements and a backtrace. If you downloaded it, installed and then ran Pillow over your script, the output could be interesting and hopefully point the search in a direction.

If I install the library from your repo things are working fine... I get a trace but I suspect it's not all that interesting. If I run the pillow install from master it also works. Maybe the problem is in the pip package then?

I can confirm that when I compile pillow from master with:

python3 setup.py build_ext --enable-freetype install

the bug doesn't appear, while if I just install the pip package (6.1.0) the bug is there.

Believe it or not, running the vagrant on my macOS machine works fine.

I created a docker-images branch to test buster, and it works fine.

@chaitusvk what operating system are you using?

it is 16.04
after install from source it is working
problem is i have not installed old version sorry
thank you....

getting same error,

>>> font = ImageFont.truetype(font_name)
>>> font
<PIL.ImageFont.FreeTypeFont object at 0x7f357f0fc4a8>
>>> font.getsize("hello")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user/anaconda3/envs/ctc/lib/python3.6/site-packages/PIL/ImageFont.py", line 162, in getsize
    size, offset = self.font.getsize(text, direction, features, language)
OSError: invalid face handle
>>> import PIL
>>> PIL.__version__
'6.0.0'
>>> 

Update

I just uninstall my pllow that was install by pip command after that I install pillow using conda command and it works.

pip uninstall pillow 
conda install pillow=6.0.0

Is there any problem with pip version of pillow ?

I met this bug in ubuntu 18.04.4 LTS, after i installed raqm by apt.
Sorry that i forget the version of my pillow that time. Then i upgrade pillow by conda to 7.0.0 and found it worked well. but i realized that my libraqm version is 0.3.0 and can't work.
then i download libraqm-0.7.0 from packages.ubuntu.com and compiled it.

Now everything is OK. Hope to help someone.

error infomation followed

File "/home/wechat/.local/lib/python3.7/site-packages/wordcloud/wordcloud.py",
    return self.generate_from_frequencies(frequencies)
 File "/home/wechat/.local/lib/python3.7/site-packages/wordcloud/wordcloud.py",
    max_font_size=self.height)
 File "/home/wechat/.local/lib/python3.7/site-packages/wordcloud/wordcloud.py",
    box_size = draw.textsize(word, font=transposed_font)
 File "/home/wechat/.local/lib/python3.7/site-packages/PIL/ImageDraw.py", line
    return font.getsize(text, direction, features, language, stroke_width)
 File "/home/wechat/.local/lib/python3.7/site-packages/PIL/ImageFont.py", line
    w, h = self.font.getsize(text)
 File "/home/wechat/.local/lib/python3.7/site-packages/PIL/ImageFont.py", line
    size, offset = self.font.getsize(text, direction, features, language)
OSError: invalid face handle

I am facing this problem since I upgraded to Pillow 7.1.2 (was earlier using 6.2.0)

Stack from sentry :

OSError: invalid face handle
File "lokalapp/misc_content/tasks.py", line 257, in create_certificate_task
w, h = draw.textsize(text_lang, font=font_lang)
File "PIL/ImageDraw.py", line 428, in textsize
return font.getsize(text, direction, features, language, stroke_width)
File "PIL/ImageFont.py", line 262, in getsize
size, offset = self.font.getsize(text, direction, features, language)

This happens when using raqm layout engine to generate an image with unicode text on an image. just like @chaitusvk and @eroux

OS : ubuntu server 16.04
Pillow version : 7.1.2
python : 3.5.2
raqm : 0.7.0

After getting a few of these errors, the kernel crashes with a segfault!

I tried manually building Pillow from master, pypi and even via pip but I am getting compilation errors there :

building 'PIL._webp' extension
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -DHAVE_WEBPMUX -I/var/www/backend-django/Pillow-master/src/libImaging -I/usr/include/x86_64-linux-gnu -I/var/www/backend-django/Pillow-master -I/usr/include/freetype2 -I/var/www/backend-django/venv/include -I/usr/local/include -I/usr/include -I/var/www/backend-django/venv/include -I/usr/include/python3.5m -c src/_webp.c -o build/temp.linux-x86_64-3.5/src/_webp.o
In file included from src/_webp.c:9:0:
/usr/include/webp/mux.h:97:13: error: expected identifier or ‘(’ before ‘int’
 WEBP_EXTERN(int) WebPGetMuxVersion(void);
             ^
/usr/include/webp/mux.h:103:13: error: expected identifier or ‘(’ before ‘WebPMux’
 WEBP_EXTERN(WebPMux*) WebPNewInternal(int);
             ^
/usr/include/webp/mux.h:116:13: error: expected identifier or ‘(’ before ‘void’
 WEBP_EXTERN(void) WebPMuxDelete(WebPMux* mux);
             ^
/usr/include/webp/mux.h:122:13: error: expected identifier or ‘(’ before ‘WebPMux’
 WEBP_EXTERN(WebPMux*) WebPMuxCreateInternal(const WebPData*, int, int);
             ^
/usr/include/webp/mux.h:159:13: error: expected identifier or ‘(’ before ‘WebPMuxError’
 WEBP_EXTERN(WebPMuxError) WebPMuxSetChunk(
             ^
/usr/include/webp/mux.h:175:13: error: expected identifier or ‘(’ before ‘WebPMuxError’
 WEBP_EXTERN(WebPMuxError) WebPMuxGetChunk(
             ^
/usr/include/webp/mux.h:188:13: error: expected identifier or ‘(’ before ‘WebPMuxError’
 WEBP_EXTERN(WebPMuxError) WebPMuxDeleteChunk(

Any help fixing this is appreciated

After getting a few of these errors, the kernel crashes with a segfault!

I tried manually building Pillow from master, pypi and even via pip but I am getting compilation errors there :

`` building 'PIL._webp' extension x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -DHAVE_WEBPMUX -I/var/www/backend-django/Pillow-master/src/libImaging -I/usr/include/x86_64-linux-gnu -I/var/www/backend-django/Pillow-master -I/usr/include/freetype2 -I/var/www/backend-django/venv/include -I/usr/local/include -I/usr/include -I/var/www/backend-django/venv/include -I/usr/include/python3.5m -c src/_webp.c -o build/temp.linux-x86_64-3.5/src/_webp.o In file included from src/_webp.c:9:0: /usr/include/webp/mux.h:97:13: error: expected identifier or ‘(’ before ‘int’ WEBP_EXTERN(int) WebPGetMuxVersion(void); ^ /usr/include/webp/mux.h:103:13: error: expected identifier or ‘(’ before ‘WebPMux’ It looks like you have WebP installed, but not the header files. You can install it on Ubuntu withsudo apt-get install libwebp-dev, or disable it with the build flag--disable-webp`. See https://pillow.readthedocs.io/en/stable/installation.html#building-on-linux for more information about building from source.

what helped me was adding parameter layout_engine=ImageFont.LAYOUT_BASIC to .text function call.

Crashing:

fnt = ImageFont.truetype('../fonts/georgiai.ttf', 36)

Not Crashing:

fnt = ImageFont.truetype('../fonts/georgiai.ttf', 36, layout_engine=ImageFont.LAYOUT_BASIC)

Another thing is to check if the error is due to the environment language setting

apt-get install -y locales
locale-gen en_US.UTF-8
export LC_ALL=en_US.UTF-8 

My error goes away after I set up en_US.UTF-8.

Reproduced on GHA: https://github.com/nulano/Pillow/actions/runs/234697378
I suspect this issue is caused by a mismatch in system FreeType and wheel FreeType ABI on the following lines:
https://github.com/python-pillow/Pillow/blob/4174d4267616897df3746d315d5a2d0f82c656ee/src/_imagingft.c#L472-L475

Edit: The logs expired, re-run with 8.0.1 added: https://github.com/nulano/Pillow/actions/runs/419599965
I also ran the function in a loop to reproduce #4225 as well (click through to individual jobs to see if they fail immediately or after a few iterations).

I also had this issue. Fixed by

pip uninstall pillow
python3 -m pip install -U https://github.com/python-pillow/Pillow/archive/master.zip

Could we please have a fix for this in Pillow? As I see it, there are reported and verifiable segfaults in Pillow on a basic operation for three years, and a working fix has been suggested half a year ago. Still this has been neither fixed nor documented, and people from dozens of projects have to spend time googling the issue (see the stream of mentions above)? As I see it, simply changing the default value would work.

@Zverik It's not a default value issue. The potential core underlying issue was identified in late November, and is a packaging/licensing issue where portions of the code will use the system Freetype library and portions using the packaged library.

Oh, right, I see this error is also discussed in #4225 and the fix is drafted in #5062. Thanks for the tip @wiredfool, let's hope the fix works :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hxzhao527 picture hxzhao527  ·  4Comments

boskicthebrain picture boskicthebrain  ·  4Comments

SysoevDV picture SysoevDV  ·  3Comments

FlowerCode picture FlowerCode  ·  4Comments

maxhumber picture maxhumber  ·  3Comments