By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,274 Members | 2,281 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,274 IT Pros & Developers. It's quick & easy.

PIL & TIFF transparency?

P: n/a
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.
--
Robin Becker
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Robin Becker <ro***@jessikat.fsnet.co.uk> wrote in message news:<bP**************@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.
Jul 18 '05 #2

P: n/a
In article <a4**************************@posting.google.com >, Myles
<my***@geocities.com> writes
Robin Becker <ro***@jessikat.fsnet.co.uk> wrote in message news:<bPFXEJAEJ7J$Ewf
Y@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
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.