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

UnicodeEncodeError: 'latin-1' codec can't encode characters in position 0-2: ordinal not in range(256) while trying to write Japanese in a new image #6225

Closed
caozhen-zen opened this issue Apr 19, 2022 · 8 comments
Labels
Anaconda Issues with Anaconda's Pillow Fonts

Comments

@caozhen-zen
Copy link

caozhen-zen commented Apr 19, 2022

What did you do?

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
image = Image.open('test_image1.png')
image_output = Image.new('RGB', image.size, "white")
draw = ImageDraw.Draw(image_output)
draw.text([203, 123], '道の駅', fill='#355C31')

What did you expect to happen?

I expect the codes to return a new image with '道の駅' written.

What actually happened?

UnicodeEncodeError: 'latin-1' codec can't encode characters in position 0-2: ordinal not in range(256)

What are your OS, Python and Pillow versions?

  • OS: macOS Big Sur; Version 11.3.1; conda 4.9.2
  • Python: Python 3.9.1
  • Pillow: 9.1.0

image
Btw, the sys.stdout.encoding is UTF-8.

Thank you for your response!

@nulano @radarhere

@nulano
Copy link
Contributor

nulano commented Apr 19, 2022

You are using the default font, which supports only Latin-1 characters. You have to use a different font, for example:

from PIL import ImageFont
font = ImageFont.truetype("NotoSansJP-Regular.ttf", size=20)
# ...
draw.text((203, 123), '道の駅', fill='#355C31', font=font)

Edit: replaced Arial (I don't think it has Japanese support) with NotoSansJP (supports Japanese)

@caozhen-zen
Copy link
Author

Yes, @nulano, with indicating the font, the codes won't raise an error while the output is messy, only three rectangles are displayed as the attached image.

image

@nulano
Copy link
Contributor

nulano commented Apr 19, 2022

Two things to check:

Does the font you used support Japanese (e.g. in an office text editor)? What font did you try?

I'm not sure if it is needed for Japanese, but do you have Raqm enabled?
You can find out by running python3 -m PIL, near the top of the output.

@caozhen-zen
Copy link
Author

caozhen-zen commented Apr 19, 2022

The previous result was generated with "Arial.ttf". Now I changed to "MSMINCHO.TTF" and now it works! Appreciate your patience and knowledge @nulano.

The first time heard abt Raqm, will go to check it out.

Btw, do u happen to know if it's possible to make the display of text a ratio of the image size?

@nulano
Copy link
Contributor

nulano commented Apr 19, 2022

You can change the size of the font by changing the size parameter when creating it. If you want to select a font size based on the image or text, you have to calculate it first then recreate the font. The following untested example might guide you in the right direction:

text = "Hello world"
target_width = 200

size = 20  # initial guess
font = ImageFont.truetype("font.ttf", size=size)
for i in range(3):  # tweak number of loops until you get satisfactory speed/quality ratio (even 1 might be enough)
    text_length = font.getlength(text)
    size = int(size * (target_width / text_length))  # update guess
    font = ImageFont.truetype("font.ttf", size=size)  # recreate font using new guess

draw.text((x, y), text, font=font, ...)

@nulano
Copy link
Contributor

nulano commented Apr 19, 2022

The first time heard abt Raqm, will go to check it out.

Since you are on MacOS, the discussion in #6175 might be relevant for installing Raqm.
Note also that IIRC the conda build of Pillow doesn't support Raqm.

@radarhere
Copy link
Member

@caozhen-zen is there anything further that we can help you with, or can this issue be closed?

@caozhen-zen
Copy link
Author

caozhen-zen commented Apr 27, 2022

Hi @radarhere , Many thx for your help. I am cool now. I think we can close this issue.

@aclark4life aclark4life added the Anaconda Issues with Anaconda's Pillow label May 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Anaconda Issues with Anaconda's Pillow Fonts
Projects
None yet
Development

No branches or pull requests

4 participants