[PIL]: Question On Changing Colour

Terry Hancock hancock at anansispaceworks.com
Tue Oct 11 21:34:48 EDT 2005


On Wednesday 12 October 2005 05:28 pm, Andrea Gavana wrote:
> Now my question: is it possible to transform the pixels 
colours in order to have another basic colour (say blue)? 
In other words, the predominant colour will become the 
blue, with other pixels in a brighter or darker blue to 
give the same 3D effects.

Have a look at the colorsys module. This will allow you
to define colors in HSV space, then convert to RGB for
image generation.  What you want to do sounds like changing
only the Hue (H) of the colors.

PIL will provide means for efficiently mapping base colors
to target colors (IIRC, you'll probably use the 'point'
method of Image objects, but check the manual).

Once you have that, you can just decide on hues for each
button, then do an individual replacement like this:

new_hue   # your 'basic color', just the hue part
rgb_base  # color from the basic button image
rgb_new   # the new color you want to replace rgb_base with

rgb_new = hsv_to_rgb( (new_hue,) + rgb_to_hsv(rgb_base)[1:])

Cheers,
Terry

--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks  http://www.anansispaceworks.com




More information about the Python-list mailing list