How to draw a rectangle with gradient?

Pontus Ekberg herrekberg at users.sf.net
Sat Sep 16 17:01:20 EDT 2006


Daniel Mark wrote:

> Hello all:
> 
> I am using PIL to draw a rectangle filled with color blue.
> 
> Is there anyway I could set the fill pattern so that the drawn
> rectangle could be filled with
> gradient blue?
> 
> 
> Thank you
> -Daniel

I don't think there is a built-in gradient function, but you could do
something like this:

>>> import Image
>>> import ImageDraw
>>> im = Image.new("RGB", (100, 100))
>>> draw = ImageDraw.Draw(im)
>>> for l in xrange(100):
...     colour = (0, 0, 255 * l / 100)
...     draw.line((0, l, 100, l), fill=colour)
...
>>> im.show()

You have to adjust for the size and colours you want of course, and change
the loop so that the gradient is in the right direction.

// Pontus



------------ And now a word from our sponsor ----------------------
For a quality mail server, try SurgeMail, easy to install,
fast, efficient and reliable.  Run a million users on a standard
PC running NT or Unix without running out of power, use the best!
----  See http://netwinsite.com/sponsor/sponsor_surgemail.htm  ----



More information about the Python-list mailing list