Search code examples
pythondrawimaging

Define color of line drawn with Python Imaging Library from an RGB tuple?


I am trying to simply draw a vertical line and define the color based on an RGB tuple. ex. (100, 200, 80). I do not see anything in the Python Imaging Library that will allow me to define the color based on the tuple. Is there something I am missing in the library, or another library, that will allow me to define the color base on the RGB values?

I have already tried using

finalImage = Image.new("RGB", (1000, 20000))
finalImageDraw = ImageDraw.Draw(finalImage)
finalImageDraw.line([(i, 0), (i, 1000)], fill=avgColor)

Where avgColor is a tuple. ie. (100, 75, 110). But it only draws black lines. Is this the correct usage?


Solution

  • use the fill argument on the draw.line method.

    You can use RGB, HSV or even named colours.

    Example (from the documentation)

    import Image, ImageDraw
    
    im = Image.open("lena.pgm")
    
    draw = ImageDraw.Draw(im)
    
    # Fill=128 creates a grey line
    draw.line((0, 0) + im.size, fill=128)   
    draw.line((0, im.size[1], im.size[0], 0), fill=128)
    del draw
    
    # write to stdout
    im.save(sys.stdout, "PNG")