【问题标题】:How can I pass variable in draw.text() function instead of string? [duplicate]如何在 draw.text() 函数中传递变量而不是字符串? [复制]
【发布时间】:2018-12-28 11:53:29
【问题描述】:

我想传递整数值而不是字符串,比如 a = 45,但它不起作用!

import Image
import ImageDraw
import ImageFont


width = disp.width
height = disp.height
image = Image.new('1', (width, height))

font = ImageFont.load_default()
a = 45

draw.text((x, top), a ,  font=font, fill=255)
draw.text((x, top+20), 'World!', font=font, fill=255)

【问题讨论】:

    标签: python python-imaging-library raspberry-pi3


    【解决方案1】:

    使用 draw.text((x, top), str(a) , font=font, fill=255) 将 a 转换为字符串。

    您也可以使用a = '45' 将其定义为字符串。但是我建议不要这样做,因为您可能希望在绘制它之前将其用作整数。

    【讨论】:

    • 但是我在这个变量中得到了实时数据,所以我不能把它变成字符串
    • 使用 str(a) 将获取 a 的实时值并返回它的 str 表示,它不会影响变量 a 的值,因此应该使用实时数据。
    • 谢谢!工作正常:)
    猜你喜欢
    • 1970-01-01
    • 2021-02-15
    • 2011-08-22
    • 2011-02-03
    • 1970-01-01
    • 1970-01-01
    • 2021-04-28
    • 2013-09-03
    • 1970-01-01
    相关资源
    最近更新 更多