镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

FreeType error when drawing text #7290

Closed

Description

@nissansz

win10, python 3.8, pillow 10, draw text sometimes has error as below. How to solve?

 draw.text((x, y), char, fill=fontcolor, font=font2, stroke_width=strokewidth, stroke_fill=strokecolor)
  File "C:\Program Files\Python38\lib\site-packages\PIL\ImageDraw.py", line 520, in text
    draw_text(ink)
  File "C:\Program Files\Python38\lib\site-packages\PIL\ImageDraw.py", line 463, in draw_text
    mask, offset = font.getmask2(
  File "C:\Program Files\Python38\lib\site-packages\PIL\ImageFont.py", line 769, in getmask2
    size, offset = self.font.getsize(
OSError: invalid reference

Activity

  1. changed the title [-]win10, python 3.8, pillow 10, draw text sometimes has error as below.[/-] [+]win10, python 3.8, pillow 10, draw text sometimes has error as below[/+] on Jul 17, 2023
  2. radarhere commented on Jul 17, 2023

    @radarhere
    Member

    Hi. It looks like this is an error from FreeType, a dependency that Pillow uses to render text - https://freetype.org/freetype2/docs/reference/ft2-error_code_values.html

    You have said that the error is intermittent, which tends to be a hard type of problem to solve. To make life easier, a self-contained example could be helpful - code that we can run to trigger the problem, and all input files that the code uses. This would let us know what font you are using, and what text you are rendering.

    It would also help to know how often the error occurs, and if there is any kind of pattern to when it occurs.

  3. changed the title [-]win10, python 3.8, pillow 10, draw text sometimes has error as below[/-] [+]Intermittent "OSError: invalid reference" when drawing text[/+] on Jul 17, 2023
  4. nissansz commented on Jul 17, 2023

    @nissansz
    Author

    I collected many fonts, but forgot to print the error font name.
    Now, I am using try catch to ignore the error.
    Will let you know if I can capture font name next time.

  5. nissansz commented on Jul 17, 2023

    @nissansz
    Author

    方正悠黑系列_502L.zip
    Here is a sample font, which caused the error.

    from PIL import Image, ImageFont, ImageDraw
    
    
    temp_canvas = Image.new("RGBA", (1200, 300), (255, 255, 255, 255))
    draw_canvas = ImageDraw.Draw(temp_canvas, "RGBA")
    
    path1 = r'F:\fonts\en\segoeui.ttf'
    font = ImageFont.truetype(path1, size=50)
    
    path2 = r'L:\paddle\字体制作\字体1000+款\01方正字体 (508款)\方正藏文新白体.TTF'
    path2 = r'L:\paddle\字体制作\字体1000+款\01方正字体 (508款)\方正悠黑系列_512B.ttf'
    path2 = r'L:\paddle\字体制作\字体1000+款\01方正字体 (508款)\方正悠黑系列_502L.ttf'
    backup_font = ImageFont.truetype(path2, size=50)
    path3 = r'F:\fonts\tianshiyanti2.0.ttf'
    backup_font2 = ImageFont.truetype(path3, size=50)
    font_family = ImageFont.FreeTypeFontFamily(backup_font, backup_font2)
    
    text_string = "hello ಠಠ world"
    text_string = "大家にほn中国制造12345잔치상 차렸어? 方正中倩简体.ttf"
    
    draw_canvas.text((100, 100), text_string, fill="#000000", font=font_family)
    
    temp_canvas.show()
  6. radarhere commented on Jul 17, 2023

    @radarhere
    Member

    I see that you're using FreeTypeFontFamily. This is a feature that only exists in #6926

    Does this problem still happen in normal Pillow, not one that you've built from #6926?

  7. nissansz commented on Jul 17, 2023

    @nissansz
    Author

    I need to use FreeTypeFontFamily. Other versions don't have this function, don't know whether there is such error.

  8. radarhere commented on Jul 17, 2023

    @radarhere
    Member

    I've tested with normal Pillow, and I find that the error still occurs.

    from PIL import Image, ImageFont, ImageDraw
    
    im = Image.new("RGB", (100, 100))
    draw = ImageDraw.Draw(im)
    font = ImageFont.truetype('方正悠黑系列_502L.ttf')
    draw.text((0, 0), "test", font=font)

    I can confirm that it is indeed coming from FreeType. I expect the font is broken.

    If you go to https://cloudconvert.com/ttf-converter and convert 方正悠黑系列_502L.ttf to an OTF font, and use that font instead, then it should work. I expect something in that conversion process has fixed the mistake in the font.

  9. changed the title [-]Intermittent "OSError: invalid reference" when drawing text[/-] [+]"OSError: invalid reference" when drawing text[/+] on Jul 17, 2023
  10. nissansz commented on Jul 17, 2023

    @nissansz
    Author

    thank you for the info. I will try to convert failed fonts.
    Will this function be included in new pillow?

  11. radarhere commented on Jul 17, 2023

    @radarhere
    Member

    I can't say when/if #6926 will be included in Pillow. It is not actually complete yet - it is still marked as a draft.

  12. radarhere commented on Jul 17, 2023

    @radarhere
    Member

    If we have given you all of that the help that you need, you can let us know by closing this issue.

  13. nissansz commented on Jul 17, 2023

    @nissansz
    Author

    ok. Thank you.

  14. nissansz commented on Jul 17, 2023

    @nissansz
    Author

    I got another error sometimes.
    For example,
    font1: Tetra decorative.otf
    font2: Noto

    string = "大家にほn中国制造12345잔치상 차렸어? 方正中倩简体.ttf"
        draw_canvas.text((x, y), text_string , fill="#000000", font=font_family, stroke_width=2, stroke_fill=(0, 0, 0))
      File "D:\Python3.8.8\lib\site-packages\PIL\ImageDraw.py", line 514, in text
        draw_text(stroke_ink, stroke_width)
      File "D:\Python3.8.8\lib\site-packages\PIL\ImageDraw.py", line 463, in draw_text
        mask, offset = font.getmask2(
      File "D:\Python3.8.8\lib\site-packages\PIL\ImageFont.py", line
    [Tetra decorative.zip](https://github.com/python-pillow/Pillow/files/12074355/Tetra.decorative.zip)
     1155, in getmask2
        self.font.render(
    OSError: array allocation size too large
  15. radarhere commented on Jul 18, 2023

    @radarhere
    Member

    That is also an error coming from FreeType. We recently had a report of this in #7287. I asked the FreeType support mailing list, and it turned out that the font was too complex. I would guess that the same thing is happening here.

    I suggest you first figure out which font is causing the problem. Try running your code with only one of the fonts at a time, and see when the error occurs.

    If you think that FreeType should work with this font as it currently exists, you can e-mail freetype@nongnu.org (as per https://freetype.org/contact.html) to ask the FreeType community about it. If you would like help putting together that e-mail, please post the font files so that we can replicate the problem. There are many variations of the Noto font, so I don't know which one you're referring to specifically. However, as I said, the last time we contacted FreeType about a font with this error, they simply said it was there were too many points.

    Alternatively, you could contact whoever created the font, and see if they are aware of this problem and if they are willing to update their font to be simpler.

  16. changed the title [-]"OSError: invalid reference" when drawing text[/-] [+]FreeType error when drawing text[/+] on Jul 18, 2023
  17. nissansz commented on Jul 18, 2023

    @nissansz
    Author

    Thank you.
    I used NotoOldShapeNormal.TTF

    For some fonts, even after converted to otf, it does not work.

    And it seems some characters do not work, some may work.
    Not sure how to judge by char or string.

  18. radarhere commented on Jul 18, 2023

    @radarhere
    Member

    I'm unable to find NotoOldShapeNormal on Google. Are able to upload the font here? And Tetra decorative as well?

  19. nissansz commented on Jul 18, 2023

    @nissansz
    Author

    Attached some fonts for you to try. You can use any other backup font you like, or don't use backup.
    Strokewidth = 0 may solve the problem for some fonts, but not all.

    otf.zip

  20. radarhere commented on Jul 18, 2023

    @radarhere
    Member

    From the fonts you've attached

    • 方正悠黑系列_512B.ttf gives "invalid reference". However, this can be worked around by converting the font to OTF
    • If I try and draw the "大家にほn中国制造12345잔치상 차렸어? 方正中倩简体.ttf" string with 懐訪体.ttf and 天崩地裂-懐訪体.ttf, I get "Bitmap missing for glyph". This is actually a Pillow error, not a FreeType. However, I'm guessing that Perform font fallback #6926 is intended to work around this.

    The scope of this issue is increasing. It was originally about one font, now it is about at least five. It was originally about one error, now it is about at least two errors.

    What I'm trying to say is that we can't help you make every font work. Some fonts are broken. You are free to contact FreeType on your own about them if you think that FreeType should support them.

    Also, I don't think it's correct for us to try and solve your problems with #6926 here. That PR hasn't been merged yet. This means it is not ready, and you are using it at your own risk. If you think you have found a problem with that PR, that you think is not simply a broken font, you can comment there to offer helpful feedback as the PR progresses. But I don't think it is reasonable to expect it to always work, as it is not finished yet.

  21. nissansz commented on Jul 18, 2023

    @nissansz
    Author

    Totally understand.
    I will exclude failed fonts for the time being and wait for final pr.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions