1

I have the following code:

im = Image.new("RGBA", (800,600))
draw = ImageDraw.Draw(im,"RGBA")
draw.polygon([(10,10),(200,10),(200,200),(10,200)],(20,30,50,125))
draw.polygon([(60,60),(250,60),(250,250),(60,250)],(255,30,50,0))
del draw 
im.show()

but the polygons do not exhibit any variance in alpha/transparency between. Is it possible to do this using these polygons or does the alpha level only apply to composited images (I'm aware of this solution but only see comments based on PIL and thought that I had seen this fixed in Pillow).

If such a thing is not available is there an nice, easy, efficient way of putting something like this in the library?

user1170304
  • 315
  • 1
  • 3
  • 12

2 Answers2

5

According to the doc http://effbot.org/imagingbook/image.htm:

im.show()

Displays an image. This method is mainly intended for debugging purposes.

On Unix platforms, this method saves the image to a temporary PPM file, and calls the xv utility.

And as far as I know, the PPM file format does not support transparency ("alpha channel").


So... the transparency does not appears when you call im.show() -- but it will be applied if you save your file using a format that does support transparency:

from PIL import Image,ImageDraw

im = Image.new("RGBA", (800,600))
draw = ImageDraw.Draw(im,"RGBA")
draw.polygon([(10,10),(200,10),(200,200),(10,200)],(20,30,50,125))
draw.polygon([(60,60),(250,60),(250,250),(60,250)],(255,30,50,120))
del draw

im.save("out.png") # Save to file
Sylvain Leroux
  • 50,096
  • 7
  • 103
  • 125
  • And even if PPM did support transparency, if you're displaying an image with partial transparency on a solid grey window… you're just going to get partially grey pixels. – abarnert Aug 17 '13 at 01:16
  • Anyway, there was a PPM-with-transparency exception, but it never went anywhere, like the 16bpp extension. If you want anything fancy, you have to use PAM instead. And nobody does, because it's easier to just use something like PNG for interchange and a custom format for internal storage. – abarnert Aug 17 '13 at 01:27
  • 1
    This method only applies the alpha to the final image, that is, a transparent square is drawn on top of another transparent square but the square beneath is still obscured. Although at least now I have transparency thanks. – user1170304 Aug 17 '13 at 11:21
  • I was having trouble with the transparency not being applied to both polygons correctly, changing the image type to "RGB" fixed it: im = Image.new("RGB", (800,600)) – Adam S. Mar 28 '14 at 23:10
0

This might help:

How do you draw transparent polygons with Python?

For adding an alpha channel to the final image you can use Image.putalpha(alpha).

Community
  • 1
  • 1
Matt
  • 781
  • 5
  • 4