[Image-SIG] Antialiased text on transparent image fails?

Laszlo Nagy gandalf at shopzeus.com
Tue Sep 11 13:45:53 CEST 2007


 Hi All,

I tried to put a text on a transparent image. Here is a test:

import Image
import ImageDraw
import ImageFont

img = Image.new('RGBA',(100,20),(255,0,0,0))
drawer = ImageDraw.Draw(img)
fnt = ImageFont.truetype("Vera.ttf",20) # 
http://ftp.gnome.org/pub/GNOME/sources/ttf-bitstream-vera/1.10/
drawer.text((0,0),"ABCDE",font=fnt,fill="#00ff00")
img.save("test.png") # Result image

I'm working on a rendering engine but I created this example to show the 
problem. The rendering engine should be able to create an image with 
transparent parts, and one should be able to put the rendered image on 
top of any other image.

The problem itself: font edges are interpolated between the background 
and foreground color. You can see it on the resulting image. If you open 
the created "test.png" file in GIMP and use the color picker tool then 
you can see values like:

(203,52,0,52)
(215,40,0,40)


I think that this is bad. The background was fully transparent. If you 
put a green object on a fully transparent thing, you should never see 
any red in it. I believe that the result should be something like

(0,52,0,52)
(0,40,0,40)

In other words, when antialiasing a text, the background pixel's color 
should be weighted with its transparency. In my example, the background 
pixel is fully red but should have zero weight.

Workarounds?

Working with black or white initial background is not a workaround, 
because PIL will darken/lighten the pixels. I used red+green just to 
make the problem more visible. A correct workaround is to use the actual 
target background that will finally be used, but it is not a good 
workaround. This is obvious: I want to render the result image once, 
then put it on different target images. Rendering the result image each 
time I need to put it on a target would be very slow.

Can you please confirm if this is a bug in PIL?  Comments welcome.

Best,

   Laszlo

-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.png
Type: image/png
Size: 1524 bytes
Desc: not available
Url : http://mail.python.org/pipermail/image-sig/attachments/20070911/29ca4ada/attachment.png 


More information about the Image-SIG mailing list