Skip to content
This repository has been archived by the owner on Jun 15, 2024. It is now read-only.

PIL OSError when font is missing #9

Open
sam210723 opened this issue Feb 11, 2020 · 5 comments
Open

PIL OSError when font is missing #9

sam210723 opened this issue Feb 11, 2020 · 5 comments
Assignees
Labels
bug Something isn't working

Comments

@sam210723
Copy link
Owner

sam210723 commented Feb 11, 2020

PIL throws an OSError when it can't load a font

Traceback (most recent call last):
  File "enhance-ir.py", line 338, in <module>
    init()
  File "enhance-ir.py", line 82, in init
    handle_input(args.INPUT)
  File "enhance-ir.py", line 184, in handle_input
    process(input)
  File "enhance-ir.py", line 244, in process
    fnt = ImageFont.truetype("arialbd.ttf", size=38)
  File "/usr/lib/python3/dist-packages/PIL/ImageFont.py", line 280, in truetype
    return FreeTypeFont(font, size, index, encoding, layout_engine)
  File "/usr/lib/python3/dist-packages/PIL/ImageFont.py", line 145, in __init__
    layout_engine=layout_engine)
OSError: cannot open resource
@sam210723 sam210723 added the bug Something isn't working label Feb 11, 2020
@sam210723 sam210723 self-assigned this Feb 11, 2020
@sam210723
Copy link
Owner Author

Same issue after updating to Pillow 7.0.0

@sam210723
Copy link
Owner Author

sam210723 commented Feb 11, 2020

Added try/catch and warning for missing fonts in 92df164

@johachi
Copy link

johachi commented Mar 12, 2020

I have the same issue when using Windows 10 with Japanese and trying to open the font ARIAL.TTF

I'm using version 7.0.0 and handle dependencies with Poetry

Traceback (most recent call last):
  File "render_annotations.py", line 240, in <module>
    main(file_path)
  File "render_annotations.py", line 216, in main
    write_image_with_annotations(image_annotations, url, item["key"])
  File "render_annotations.py", line 182, in write_image_with_annotations
    newly_drawn_text_box = draw_annotation_to_image(annotation, image, drawn_text_boxes)
  File "render_annotations.py", line 154, in draw_annotation_to_image
    return draw_text(annotation, image, drawn_text_boxes)
  File "render_annotations.py", line 112, in draw_text
    fnt = ImageFont.truetype("ARIAL.TTF", ANNOTATION_FONT_SIZE)
  File "C:\Users\Man\AppData\Local\pypoetry\Cache\virtualenvs\qa-show-all-annotations-gvzPHQ0M-py3.7\lib\site-packages\PIL\ImageFont.py", line 642, in truetype
    return freetype(font)
  File "C:\Users\Man\AppData\Local\pypoetry\Cache\virtualenvs\qa-show-all-annotations-gvzPHQ0M-py3.7\lib\site-packages\PIL\ImageFont.py", line 639, in freetype
    return FreeTypeFont(font, size, index, encoding, layout_engine)
  File "C:\Users\Man\AppData\Local\pypoetry\Cache\virtualenvs\qa-show-all-annotations-gvzPHQ0M-py3.7\lib\site-packages\PIL\ImageFont.py", line 188, in __init__
    font, size, index, encoding, layout_engine=layout_engine
OSError: cannot open resource
PS C:\Users\Man\Documents\GitHub\crowd-ops\Projects\Add-Annotations> 

I was able to find a work-around copying the file to the same folder as the script and then changing:

fnt = ImageFont.truetype("ARIAL.TTF", ANNOTATION_FONT_SIZE)

to

fnt = ImageFont.truetype("./ARIAL.TTF", ANNOTATION_FONT_SIZE)

@sam210723
Copy link
Owner Author

Ok interesting, this seems to be a bit tedious to get going cross-platform. I'll investigate.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants