1

I had this code, which gives png file containing coresponding text2write variable.

# -*- coding: utf-8 -*-
# text 2 img

from PIL import Image, ImageDraw, ImageFont

bg={
'white':(255, 255, 255),
'black':(0,0,0),
'black':'black',
'grey':(125,125,125)
}

ftsize=30
# text2write
# 見五口
text=u"見五口"

# dynamic bg size by text size
bgsize=(int((ftsize*len(text))/2+ftsize),ftsize*2) #x,y

img = Image.new('RGB', bgsize, color = bg['grey'])
# usethis if py2
fnt = ImageFont.truetype('/home/user/.fonts/arial.ttf', ftsize)
# fnt = "arial"
d = ImageDraw.Draw(img)
d.text((10,10), text,
font=fnt, fill=bg['black'])
 
img.save('output.png')

gives this output.

output

I do expect 見五口 chars shows in the png.

adib-enc
  • 433
  • 1
  • 5
  • 6

2 Answers2

2

Try with this, when you put text on image

text.encode("utf-8")

Edit2

fnt=ImageFont.truetype("/Library/Fonts/Arial Unicode.ttf",14)
draw.text((50, 50), text, font=font)
Wonka
  • 1,548
  • 1
  • 13
  • 20
  • Found this answer, check it, probably is the font: https://stackoverflow.com/questions/11411746/drawing-multilingual-text-using-pil – Wonka Oct 14 '19 at 14:59
  • Thanks, combining [el hafizh hidayat](https://stackoverflow.com/users/11284018/el-hafizh-hidayat) answer make it work! – adib-enc Oct 14 '19 at 16:08
2

the font doesn't support unicode though. Use Conda200 instead, it works

fnt = ImageFont.truetype('/home/user/.local/share/fonts/CODE2000.TTF', ftsize)
  • Please edit the information that you think will be useful into your answer. Simply including a link can be problematic if the URL changes over time. – Dodge Oct 14 '19 at 16:04