Re: [PIL]: Question On Changing Colour

Discussion in 'Python' started by Terry Hancock, Oct 12, 2005.

  1. 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
     
    Terry Hancock, Oct 12, 2005
    #1
    1. Advertising

  2. Terry Hancock

    Iain King Guest

    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
     
    Iain King, Oct 14, 2005
    #2
    1. Advertising

  3. 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
     
    Andrea Gavana, Oct 14, 2005
    #3
  4. <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
     
    Scott David Daniels, Oct 14, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Craig

    Opening colour BMPs with PIL

    Craig, Dec 4, 2006, in forum: Python
    Replies:
    2
    Views:
    525
    Craig
    Dec 6, 2006
  2. Johny

    PIL and font colour

    Johny, Apr 24, 2007, in forum: Python
    Replies:
    0
    Views:
    265
    Johny
    Apr 24, 2007
  3. Johny

    PIL and font colour

    Johny, Apr 24, 2007, in forum: Python
    Replies:
    3
    Views:
    393
    Steve Holden
    Apr 26, 2007
  4. Johny

    How to set font colour in PIL

    Johny, Apr 25, 2007, in forum: Python
    Replies:
    0
    Views:
    272
    Johny
    Apr 25, 2007
  5. jodleren
    Replies:
    2
    Views:
    218
    Thomas 'PointedEars' Lahn
    Jan 12, 2008
Loading...

Share This Page