Search code examples
pythonpython-imaging-libraryhsl

Image.putpixel with HSL value: TypeError "argument is not tuple"


I want to put HSL data into an RGBA image. This is my code:

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)

This gives me the following error for the putpixel function: TypeError: new style getargs format but argument is not a tuple. However, hsl_String is hsl(180,100%,50%), similar to what is stated in the PIL documentation.

Replacing hsl_String by e.g. (0,0,0,0) works well, as well as replacing by (0,0,0) (an RGB value without opacity despite the image is RGBA).

What does that error mean? And how can I add a value for the opacity (the alpha channel) to an HSL value at all?


Solution

  • Try using getrgb function from ImageColor module:

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

    You can add alpha value like this:

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