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: invalid argument when draw stroke with specific font #5759

Closed
mirusu400 opened this issue Oct 13, 2021 · 1 comment · Fixed by #5761
Closed

OSError: invalid argument when draw stroke with specific font #5759

mirusu400 opened this issue Oct 13, 2021 · 1 comment · Fixed by #5761

Comments

@mirusu400
Copy link

What did you do?

I make a simple image, draw with Image, ImageFont, ImageDraw class.

What did you expect to happen?

I just want to add stroke.

What actually happened?

It gets error with specific font when I add stroke width.

What are your OS, Python and Pillow versions?

  • OS: Windows-10-10.0.17763-SP0
  • Python: 3.9.7 (tags/v3.9.7:1016ef3, Aug 30 2021, 20:19:38) [MSC v.1929 64 bit (AMD64)]
  • Pillow: 8.3.2
from PIL import Image, ImageDraw, ImageFont, ImageOps

img = Image.new("RGBA", (100, 100), color=(255, 255, 255, 255))
fnt = ImageFont.truetype("C:/Windows/Fonts/calibriz.ttf", 16)

draw = ImageDraw.Draw(img)
text="a"
for i in text:
    draw.text(
        (0, 0),
        i,
        fill=(255,255,0,255),
        font=fnt,
        anchor="la",
        stroke_width=2,
        stroke_fill=(255,255,255,255)
    )

img.save("test.png")
Traceback (most recent call last):
  File "C:\Users\User\Documents\_github\drawfontimage\core.py", line 180, in create
    draw.text(
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\PIL\ImageDraw.py", line 457, in text
    draw_text(stroke_ink, stroke_width)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\PIL\ImageDraw.py", line 408, in draw_text
    mask, offset = font.getmask2(
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\PIL\ImageFont.py", line 676, in getmask2
    self.font.render(
OSError: invalid argument

The OSError is not happen when we remove option stroke_width and stroke_fill .


Pillow version 7.2.0 PERPECTLY resolve this issue, but after 8.0.0 it gets error.

I make simple repo for test / explain this issue.

https://github.com/mirusu400/pillow-stroke-error

Only some of fonts get this error, but I don't know the reason.

@radarhere radarhere changed the title OSError: invalid argument when draw stroke with specific font. OSError: invalid argument when draw stroke with specific font Oct 13, 2021
@nulano
Copy link
Contributor

nulano commented Oct 13, 2021

I have found this is related to the bitmap font fix - for obvious reasons FreeType is failing to stroke bitmap glyphs.

The failing fonts have bundled bitmap glyphs that are loaded by default - adding FT_LOAD_NO_BITMAP prevents this and fixes the issue.

Edit: I'm not sure I have time to put together a PR before the release in two days. I think this will require changing both the font_render and text_layout functions, the latter getting a new parameter stroke_width to be passed from the Python layer.

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

Successfully merging a pull request may close this issue.

3 participants