字体和python类型错误:应为字符串

2024-09-25 16:25:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试通过我的应用程序创建图像,但到目前为止我没有运气

我试着用简单的脚本来创建图像,但是当我在应用程序中插入代码时,我遇到了一个奇怪的错误,我不知道为什么会这样

下面是我的简单脚本,很好用:

from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw

font = ImageFont.truetype("arial.ttf",24)
img=Image.new("RGBA", (600,200),"white")
draw = ImageDraw.Draw(img)
draw.text((400, 150),"sasa","black",font=font)
draw = ImageDraw.Draw(img)
img.save("image.png")

但是当我试图在我的web应用程序中使用它来创建图像时,我遇到了一个错误

^{pr2}$

以下是我在我的网络应用程序中的代码:

 img = Image.new("RGBA", (template_id.cheque_width, template_id.cheque_height), "white")
 draw = ImageDraw.Draw(img)
 if template_id.cheque_title_print:
           draw.text((template_id.cheque_title_margin_top, template_id.cheque_title_bottom),
                              template_id.cheque_title, "black",
                              font=ImageFont.truetype('arial.ttf', template_id.cheque_title_font_size))

#these is the values in the code
#template_id.cheque_width=600
#template_id.cheque_height=200
#template_id.cheque_title_margin_top=100
#template_id.cheque_title_bottom)=100
#template_id.cheque_title_print=True
# template_id.cheque_title="some_string"
#template_id.cheque_title_font_size=24

我知道它访问字体文件,但为什么会出现这个错误?我该怎么解决呢?在


Tags: from图像importid应用程序imgpiltitle