tags:

views:

1177

answers:

3

I'm using PIL (Python Imaging Library). I'd like to draw transparent polygons. It seems that specifying a fill color that includes alpha level does not work. Are their workarounds?

If it can't be done using PIL I'm willing to use something else.

If there is more than one solution, then performance should be factored in. The drawing needs to be as fast as possible.

+2  A: 

I'm using cairo + pycairo for this, and it works well. And you can share image data between PIL and cairo, using python buffer interface, if there is operation in pil that can't be done in cairo.

Antonin ENFRUN
+1  A: 

PIL's Image module provides a blend method.

Create a second image the same size as your first, with a black background. Draw your polygon on it (with full colour). Then call Image.blend passing the two images and an alpha level. It returns a third image, which should have a semi-transparent polygon on it.

I haven't measured the performance (hey, I haven't even tried it!) so I cannot comment on it's suitability. I suggest you work out your performance budget, and then measure it to see if it is fast enough for your purposes.

Oddthinking
+4  A: 

What I've had to do when using PIL to draw transparent images is create a color layer, an opacity layer with the polygon drawn on it, and composited them with the base layer as so:

color_layer = Image.new('RGBA', base_layer.size, fill_rgb)
alpha_mask = Image.new('L', base_layer.size, 0)
alpha_mask_draw = ImageDraw.Draw(alpha_mask)
alpha_mask_draw.polygon(self.outline, fill=fill_alpha)
base_layer = Image.composite(color_layer, base_layer, alpha_mask)

When using Image.Blend I had issues with strange outlining behaviors on the drawn polygons.

The only issue with this approach is that the performance is abysmal when drawing a large number of reasonably sized polygons. A much faster solution would be something like "manually" drawing the polygon on a numpy array representation of the image.

akdom