【问题标题】:Image.putpixel with HSL value: TypeError "argument is not tuple"带有 HSL 值的 Image.putpixel:TypeError "argument is not tuple"
【发布时间】:2016-01-25 01:09:38
【问题描述】:

我想将 HSL 数据放入 RGBA 图像中。这是我的代码:

import Image

myImage = Image.new("RGBA", (100, 100), (0,0,0,255))

h = 180
s = 100
l = 50

hsl_String = "hsl(" + str(h) + "," + str(s) + "%," + str(l) + "%)"
print hsl_String
myImage.putpixel((2, 2), hsl_String)

这给了我 putpixel 函数的以下错误:TypeError: new style getargs format but argument is not a tuple。但是,hsl_String 是 hsl(180,100%,50%),类似于 PIL 文档中的说明。

将 hsl_String 替换为例如(0,0,0,0) 效果很好,也可以用 (0,0,0) 替换(尽管图像是 RGBA,但没有不透明度的 RGB 值)。

这个错误是什么意思?以及如何将不透明度(alpha 通道)的值添加到 HSL 值?

【问题讨论】:

    标签: python python-imaging-library hsl


    【解决方案1】:

    尝试使用来自ImageColor 模块的getrgb 函数:

    myImage.putpixel((2, 2), ImageColor.getrgb(hsl_String))
    

    您可以像这样添加 alpha 值:

    alpha = 255
    myImage.putpixel((2, 2), ImageColor.getrgb(hsl_String) + (alpha, ))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-13
      • 1970-01-01
      • 2022-08-04
      • 1970-01-01
      • 1970-01-01
      • 2015-09-08
      • 2022-11-18
      • 2022-12-27
      相关资源
      最近更新 更多