PIL & TIFF transparency?

Discussion in 'Python' started by Robin Becker, Jul 30, 2003.

  1. Robin Becker

    Robin Becker Guest

    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
     
    Robin Becker, Jul 30, 2003
    #1
    1. Advertising

  2. Robin Becker

    Myles Guest

    Robin Becker <> wrote in message news:<bPFXEJAEJ7J$>...
    > 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.
     
    Myles, Jul 31, 2003
    #2
    1. Advertising

  3. Robin Becker

    Robin Becker Guest

    In article <>, Myles
    <> writes
    >Robin Becker <> wrote in message news:<bPFXEJAEJ7J$Ewf
    >>...
    >> 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
     
    Robin Becker, Jul 31, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marek

    PIL and transparency in IE

    Marek, Jun 26, 2003, in forum: Python
    Replies:
    0
    Views:
    1,083
    Marek
    Jun 26, 2003
  2. jean-Baptiste Cazier

    Transparency with PIL and paste ?

    jean-Baptiste Cazier, Jul 23, 2003, in forum: Python
    Replies:
    0
    Views:
    665
    jean-Baptiste Cazier
    Jul 23, 2003
  3. Gustavo Campanelli

    Newbie : Gif transparency with Pil problem.

    Gustavo Campanelli, Dec 15, 2003, in forum: Python
    Replies:
    2
    Views:
    356
    Gustavo Campanelli
    Dec 16, 2003
  4. Caleb Hattingh
    Replies:
    2
    Views:
    2,169
    Caleb Hattingh
    Dec 1, 2004
  5. ifiaz
    Replies:
    1
    Views:
    1,050
    Martien Verbruggen
    Nov 19, 2003
Loading...

Share This Page