【问题标题】:Can't use variables to define color with Pillow不能使用变量来定义 Pillow 的颜色
【发布时间】:2017-09-04 14:08:35
【问题描述】:

我在使用 Pillow 时遇到了一个小(我猜)错误。我正在尝试使用变量设置文本的颜色,但在尝试时得到 ValueError: unknown color specifier: '"232,43,123,255"'。 这是我的代码:

header = Image.open("A.png")
couleur_Ellipse = "rose"
draw = ImageDraw.Draw(header)
point = ( int(header.width * 0.780), int(header.height * 0.68))
header.paste(ellipse, point, ellipse)
colortext = '"232,43,123,255"'
pointtext1 = ( int(header.width * 0.845), int(header.height * 0.75))
pointtext2 = ( int(header.width * 0.798), int(header.height * 0.79))
draw.text(pointtext1, "this is some text", font=ImageFont.truetype("C:\Windows\Fonts\Arial.ttf", 14), fill=colortext)
draw.text(pointtext2, "some other text", font=ImageFont.truetype("C:\Windows\Fonts\\arialbd.ttf", 55), fill=colortext)
header.save('Images/header ' + couleur_Ellipse + '.png')

有什么见解吗?我不明白为什么它不起作用

【问题讨论】:

  • pillow.readthedocs.io/en/3.1.x/reference/ImageColor.html 似乎您需要提供颜色函数,而不仅仅是一串值。我还假设您使用的 PIL 版本等于或大于 1.1.4,以便您可以提供 alpha 值?尝试不使用 alpha:colortext = "rgb(232,43,123)"
  • 有效!非常感谢。我不是母语人士,所以文档对我来说总是有点难以阅读。谢谢!

标签: python colors pillow


【解决方案1】:

评论转化为答案:

cf:http://pillow.readthedocs.io/en/3.1.x/reference/ImageColor.html

根据文档,您需要提供颜色函数(或十六进制颜色),而不仅仅是一串值。而且,作为字符串提供的颜色函数似乎不支持 alpha 值。

试试这个:

colortext = "rgb(232,43,123)"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-21
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    • 2021-03-30
    • 1970-01-01
    • 2013-03-17
    相关资源
    最近更新 更多