How to find complementary colour for pixel

Steven D'Aprano steve at REMOVEME.cybersource.com.au
Fri Apr 27 07:09:15 EDT 2007


On Thu, 26 Apr 2007 21:17:42 -0700, James Stroud wrote:

> Johny wrote:
>>  I use PIL to write some text to a picture.The text must  be seen wery
>> clearly.
>> I write the text to different pictures but to the same position. As 
>> pictures maybe  different, colour, in the position where I write the
>> text, is also different.
>> Is there a way how to set the font colour so that it will be seen very
>> clearly in the picture?
>> For example, if the picture is bright ( for example yellow), the font
>> colour should be dark( e.g. black) and vice versa.
>> Is there a routine in PIL available  that calculates complementary
>> colour for RGB pixel format?
>> Can anyone help?
>> Thanks
>> L.
>> 
> 
> Don't you just xor with black?

I shouldn't think so... I always forget if black is all ones or all
zeroes, so I checked here: http://www.pitt.edu/~nisg/cis/web/cgi/rgb.html

>>> black = 0x000000 # RGB colours
>>> white = 0xffffff
>>> black^black == white
False
>>> white^black == black
False

Maybe you meant xor with white?

But even so, I gather that the Original Poster is asking for something
slightly different -- despite his mention of complimentary colours. (Or
maybe I'm just reading too much into it?)

I think he's just looking for one colour which has high contrast over a
bunch of pixels in an image. I don't think that can be done, in general:
it seems to me he's assuming that all the pixels in the area he wants
to overlay text on are "nearly" the same colour.

I think that a better solution is to draw a box around the area you
want to write text into. The box should have a contrasting frame:
say, a thin white border followed by a thin black border just inside it:
either the white or the black will contrast with the background, no matter
what colour(s) the background is, since no area can be both black and
white in the same place. Then fill the frame with white, and draw black
text inside it. Nice high contrast.

That is probably the most readable method of placing text over an image.


-- 
Steven D'Aprano 




More information about the Python-list mailing list