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

Crash when loading font on Japanese Windows 10 #4468

Closed
johachi opened this issue Mar 12, 2020 · 5 comments
Closed

Crash when loading font on Japanese Windows 10 #4468

johachi opened this issue Mar 12, 2020 · 5 comments
Labels

Comments

@johachi
Copy link

johachi commented Mar 12, 2020

What did you do?

Tried to draw text to an image using:

ANNOTATION_FONT_SIZE = 20
fnt = ImageFont.truetype("ARIAL.TTF", ANNOTATION_FONT_SIZE)
text = "Test Text"
text_color = (64, 148, 224)
draw.text((50, 50), text, text_color, font=fnt)

What did you expect to happen?

The text to be drawn

What actually happened?

Application Crashed

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> 

What are your OS, Python and Pillow versions?

  • OS: Windows 10 (Japanese)
  • Python: 3.7.6
  • Pillow: 7.0.0
ANNOTATION_FONT_SIZE = 20
fnt = ImageFont.truetype("ARIAL.TTF", ANNOTATION_FONT_SIZE)
text = "Test Text"
text_color = (64, 148, 224)
draw.text((50, 50), text, text_color, font=fnt)

Other

The font is installed (double-checked).

Saving the font in the same folder as the script and changing

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

to

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

is a viable work-around.

Also see sam210723/xrit-rx#9

@radarhere
Copy link
Member

radarhere commented Mar 12, 2020

Would you be able to run the following script? This is code that I've put together to print the path of the font that Pillow is trying to access. Hopefully that should provide more insight.

def printFontPath(path):
	import os
	windir = os.environ.get("WINDIR")
	if windir:
		print("Note: Searches in "+os.path.join(windir, "fonts"))
	else:
		print("Note: No WINDIR present")
	
	from PIL import ImageFont
	freeType = ImageFont.FreeTypeFont

	class FreeTypeFont(freeType):
		def __init__(self, font=None, size=10, index=0, encoding="", layout_engine=None):
			if font != path:
				print(font)
			super().__init__(font, size, index, encoding, layout_engine)

	ImageFont.FreeTypeFont = FreeTypeFont
	try:
		ImageFont.truetype(path)
	except OSError:
		pass

printFontPath("ARIAL.TTF")

@johachi
Copy link
Author

johachi commented Mar 12, 2020

Thank you for the fast follow up.

I will run the script as soon as I get back to that computer.

@johachi
Copy link
Author

johachi commented Mar 13, 2020

It seems to be looking in the correct folder:

PS C:\Users\Man\Documents\GitHub> & C:/Users/Man/AppData/Local/Programs/Python/Python37-32/python.exe c:/Users/Man/Documents/GitHub/crowd-ops\Projects\Add-Annotations/font2.py
Note: Searches in C:\WINDOWS\fonts
PS C:\Users\Man\Documents\GitHub>

@radarhere
Copy link
Member

Ah, but, my script should have also printed a path for the font. The fact that it didn't should mean that Pillow isn't finding 'ARIAL.TTF' in C:\WINDOWS\fonts.

What is the path to your installed ARIAL.TTF?

@johachi
Copy link
Author

johachi commented Mar 13, 2020

I somehow solved it.

Debugging
There is a arial.ttf file in C:\WINDOWS\fonts. That is also where windows say that the font is installed (picture 1). Also, in the script you gave me, I tried both arial.ttf and ARIAL.ttf but the result was the same for both.

I then opened the font by writing C\WINDOWS\fonts\arial.ttf in the command line. I don't know why, but the button for install was clickable (picture 2). The font installed and the path showing where the font is installed changed. (picture 3) to C:\USERS\マヨ\APPDATA\LOCAL\MICROSOFT\WINDOWS\FONTS\ARIAL.TTF. Pillow do now find the font if I write arial.ttf but not if I write ARIAL.TTF, Also, the script you gave me now prints the path where the font has been all along:

C:\Users\マ\Documents>python font2.py
Note: Searches in C:\WINDOWS\fonts
C:\WINDOWS\fonts\arial.ttf

C:\Users\マ\Documents>

Using the path in picture 3 gives the unicode error:

C:\Users\マ\Documents>python font2.py
  File "font2.py", line 24
    printFontPath("C:\USERS\マヨ\APPDATA\LOCAL\MICROSOFT\WINDOWS\FONTS\ARIAL.TTF")
                 ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 1-2: truncated \UXXXXXXXX escape

C:\Users\マヨ\Documents>

Important to note is that I could only get the "Install button" to be enabled by opening the file through the command line. Opening the preview through right click did not enable the install button.

I was able to replicate the problem/error by uninstalling the font making windows once again show that the font is installed in C:\WINDOWS\FONTS\ARIAL.TTF. I did this back and forth a few times to be sure that this was the cause. I then uninstalled it one last time. I then tried to access another font bahnschrift.ttf and that worked. Very surprised I then switched back to arial.ttf and it still works, which surprised me even more.

I would like to write this of as a "misspelled font name" BUT I am 100% sure that spelled the font name correctly in lower case letters before I tried upper case. The fact that installing and uninstalling the font made the script work/break without chaining it also proof that it was not a spelling problem together with the fact that I could run it without a problem on a mac machine. I was also able to recreate the problem a few times, so I just don't know what is going on... The reason I wrote this much is that I hope it will help anyone else who might run into the same problem in the future. I will try to reboot my PC after posting this. But if I can no longer replicate the error, then I do not know how I would be able to debug any further to find the root cause.

Thank you very much for the help @radarhere

Picture 1
Picture 1

Picture 2
Picture 2

Picture 3
Picture 3

This issue was closed.
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

2 participants