![](/img/trans.png)
[英]How do I center my text on an image using PIL in python and save the image with a different name in a for or while loop?
[英]How do i get correct output for japan text to image using PIL?
我有這段代碼,它給出了包含對應 text2write 變量的 png 文件。
# -*- 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')
給出這個 output。
我確實希望在 png 中顯示見五口字符。
試試這個,當你把文字放在圖像上時
text.encode("utf-8")
fnt=ImageFont.truetype("/Library/Fonts/Arial Unicode.ttf",14)
draw.text((50, 50), text, font=font)
該字體雖然不支持 unicode。 改用Conda200 ,它可以工作
fnt = ImageFont.truetype('/home/user/.local/share/fonts/CODE2000.TTF', ftsize)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.