PIL & TIFF transparency?

R

Robin Becker

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.
 
M

Myles

Robin Becker said:
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.
 
R

Robin Becker

Myles said:
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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top