[Image-SIG] Font colour

Douglas Bagnall douglas at paradise.net.nz
Thu Apr 26 23:21:50 CEST 2007


export at hope.cz wrote:

> Is there a way how to find out a complementary colour for an area
> where I will write the text, so that the text will be seen clearly?

As Amos Newcombe pointed out, this is not what you really want.  If
you still want to try, something like this would be easiest:

# complement the whole image
comp = im.point(lambda x: 255 - x)

# make a text mask -- a black image with white writing
mask = Image.new('L', im.size, 0)
font = ImageFont.load_default()
draw = ImageDraw.Draw(mask)
draw.text((5, 5), "hello hello", fill="white")

#paste the complementary image over the original, using the text as a
#mask.
im.paste(comp, (0,0), mask)

To follow Paul Svensson's suggestion, you would do the same, but with
a different overlay image.

inverse_threshold = im.point((([255]*128) + ([0]*128)) * 3)

and paste that instead of the complementary image.  But I think the
clearest way, based on watching subtitled movies, is to have the text
in a constant colour, with an outline, or shadow, or glow.  So I would
try instead:

blur = mask.filter(ImageFilter.BLUR)
im.paste((0,0,0), (0,0), blur)
im.paste((255,255,255), (0,0), mask)

for white writing with a black halo.  Maybe black on white is better.


douglas







More information about the Image-SIG mailing list