PIL & TIFF transparency?

Myles myles at geocities.com
Thu Jul 31 02:50:12 CEST 2003


Robin Becker <robin at jessikat.fsnet.co.uk> wrote in message news:<bPFXEJAEJ7J$EwfY 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.




More information about the Python-list mailing list