Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: [PIL]: Question On Changing Colour

Reply
Thread Tools

Re: [PIL]: Question On Changing Colour

 
 
Terry Hancock
Guest
Posts: n/a
 
      10-12-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

 
Reply With Quote
 
 
 
 
Iain King
Guest
Posts: n/a
 
      10-14-2005

Andrea Gavana wrote:
> I have tried your solution, Terry:
>
> > 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:])

>
>
> thanks a lot for your suggestion! However, either I did not understand it
> correctly or I am doing something stupid in my code. Here is a small
> example:
>
> from colorsys import *
>
> # that is the old colour --> GREY
> rgb_old = (0.7, 0.7, 0.7)
>
> # Transform the new colour in HSV
> hsv_old = rgb_to_hsv(rgb_old[0], rgb_old[1], rgb_old[2])
>
> # this is the new colour --> BLUE
> rgb_new = (0.0, 0.0, 1.0)
>
> # Transform the new colour in HSV
> hsv_new = rgb_to_hsv(rgb_new[0], rgb_new[1], rgb_new[2])
>
> # I take only the Hue part of the new colour
> new_hue = hsv_new[0]
>
> # Get the new colour
> rgb_new = hsv_to_rgb(new_hue, hsv_old[1], hsv_old[2])
>
> print rgb_old
> print rgb_new
> print rgb_old == rgb_new
>
>
> This prints:
>
> (0.69999999999999996, 0.69999999999999996, 0.69999999999999996)
> (0.69999999999999996, 0.69999999999999996, 0.69999999999999996)
> True
>
> So, no matter what colour I choose as a "new" colour, the Hue part of the
> new colour doesn't change in RGB. In other words, leaving the old value for
> "Saturation" and "Value" makes the presence of the "Hue" part useless. But
> why in the world does this happen? If a colour is defined by 3 values,
> changes in every single value should change the colour too...


Not with HSV. The hue determines which 'color' it will be - red, blue,
indigo, whatever. That Saturation determined how vibrant this 'color'
will be. V is brightness (I can't remember what the V actually stands
for). Each of these values scales from 0 to 1, or 0% to 100%, however
you want to thiink about it. If you try and picture the gradient
you'd get by plotting this range as a line, then:
The H line would be a spectrum of colours, like a rainbow.
Say we pick H to be RGB #FF0000 - Red
The S line would be a gradient ranging from grey (absense of color) to
red.
The V line would be a gradient ranging from black (completely dark) to
red.

So on the HSV scale, grey is represented by a saturation of 0 - meaning
none of H is present in the color; the color in question being
determined purely by it's brightness (V). So when you pick your HSV
triplet for a grey color, you have to set S to 0. You can set H to
anything at all - because S is 0, no tint of H will appear in the color
at all.

Iain http://www.snakebomb.com

 
Reply With Quote
 
 
 
 
Andrea Gavana
Guest
Posts: n/a
 
      10-14-2005
I have tried your solution, Terry:

> 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:])



thanks a lot for your suggestion! However, either I did not understand it
correctly or I am doing something stupid in my code. Here is a small
example:

from colorsys import *

# that is the old colour --> GREY
rgb_old = (0.7, 0.7, 0.7)

# Transform the new colour in HSV
hsv_old = rgb_to_hsv(rgb_old[0], rgb_old[1], rgb_old[2])

# this is the new colour --> BLUE
rgb_new = (0.0, 0.0, 1.0)

# Transform the new colour in HSV
hsv_new = rgb_to_hsv(rgb_new[0], rgb_new[1], rgb_new[2])

# I take only the Hue part of the new colour
new_hue = hsv_new[0]

# Get the new colour
rgb_new = hsv_to_rgb(new_hue, hsv_old[1], hsv_old[2])

print rgb_old
print rgb_new
print rgb_old == rgb_new


This prints:

(0.69999999999999996, 0.69999999999999996, 0.69999999999999996)
(0.69999999999999996, 0.69999999999999996, 0.69999999999999996)
True

So, no matter what colour I choose as a "new" colour, the Hue part of the
new colour doesn't change in RGB. In other words, leaving the old value for
"Saturation" and "Value" makes the presence of the "Hue" part useless. But
why in the world does this happen? If a colour is defined by 3 values,
changes in every single value should change the colour too...
Ah, thanks God for the existence of RGB

Thanks a lot for every suggestion.

Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.virgilio.it/infinity77


 
Reply With Quote
 
Scott David Daniels
Guest
Posts: n/a
 
      10-14-2005
<Confusion about applying hue to a grey value>
Try this:
>>> import colorsys as cs
>>> grey = (.7, .7, .7)
>>> blue = (0., 0., 1.)
>>> hsv_grey = cs.rgb_to_hsv(*grey)
>>> hsv_blue = cs.rgb_to_hsv(*blue)
>>> hsv_grey

(0.0, 0.0, 0.69999999999999996)
>>> hsv_blue

(0.66666666666666663, 1.0, 1.0)

The problem is that the saturation of the grey is 0. There
is no Hue to anything between black and white. Maybe you want
something like:

def apply_hue(color, rgb):
hue, _saturation, _value = cs.rgb_to_hsv(*color)
_hue, saturation, value = cs.rgb_to_hsv(*rgb)
return cs.hsv_to_rgb(hue, max(.1, saturation), value)

Or:

def apply_hs(color, rgb):
hue, saturation, value = cs.rgb_to_hsv(*color)
_hue, _saturation, value = cs.rgb_to_hsv(*rgb)
return cs.hsv_to_rgb(hue, saturation, value)


--Scott David Daniels

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the point of having 16 bit colour if a computer monitor can only display 8 bit colour? How do you edit 16 bit colour when you can only see 8 bit? Scotius Digital Photography 6 07-13-2010 03:33 AM
Getting default colour for <input> - or just the old colour jodleren Javascript 2 01-12-2008 02:57 PM
Datagrid - Changing row colour Simon Harris ASP .Net 1 02-10-2005 08:36 AM
Changing colour of validator error messages Helen ASP .Net 2 09-06-2004 09:41 PM
Colour blindness, photography and colour management Tor Lillqvist Digital Photography 12 05-24-2004 08:57 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57