PIL & TIFF transparency?

Robin Becker robin at jessikat.fsnet.co.uk
Thu Jul 31 10:01:40 CEST 2003

In article <a4638b9b.0307301650.65e6f08b at posting.google.com>, Myles
<myles at geocities.com> writes
>Robin Becker <robin at jessikat.fsnet.co.uk> wrote in message news:<bPFXEJAEJ7J$Ewf
>Y at jessikat.fsnet.co.uk>...
>> Has anyone done transparency with PIL & TIFF? I'm using PIL to generate
>> a preview TIFF for embedding into an eps file and am being asked for the
>> TIFF to support transparency.
>Simple example that makes pure white areas of an RGB image transparent
>(there's probably a better way to create the mask, and a better way to 
>add the mask to the image, but this gives you a starting point):
>import Image, ImageChops
>im = Image.open('/junk/pickme.png')
># create mask (will become alpha band i.e. transparency)
>mask = im.point(lambda i : i == 255 and 255)   # create RGB mask
>mask = mask.convert('L')                       # mask to grayscale
>mask = mask.point(lambda i : i == 255 and 255) # mask to B&W grayscale
>mask = ImageChops.invert(mask)
># merge mask with image
>R, G, B = im.split()
>nuimg = Image.merge('RGBA', (R, G, B, mask))
>nuimg.save('/junk/out.tif', 'tiff')
>Regards, Myles.
thanks very much, I didn't know the TIFF format even supported
transparency till yesterday and now you say the PIL tiff plugin will
handle it ok. Great work pythoneers!
Robin Becker

More information about the Python-list mailing list