[Image-SIG] Creating a Transparent Image and Placing it Over Another Image

Wayne Watson sierra_mtnview at sbcglobal.net
Tue Apr 7 02:35:17 CEST 2009


Consider this program that I pulled off the web and was posted in 1999.
It purports to illustrate how one might produce a transparency.

    #!/usr/bin/python
    # see http://mail.python.org/pipermail/python-list/1999-May/003388.html
    from Tkinter import *
    import Image, ImageTk
    import tkFileDialog

    class Transparency:
     def __init__(self, parent):
      self.canvas = Canvas(parent, bg='green')
      self.canvas.pack()
      b = Button(parent, command=self.open, text="Select graphics file")
      b.pack()

     def open(self):
      self.canvas.delete(ALL)
      filename = tkFileDialog.askopenfilename()
      if filename != '':
       im = Image.open(filename)
       if im.mode != "RGBA":
        im = Image.open(filename).convert("RGBA")
        source = im.split()
        R, G, B, A = 0, 1, 2, 3
        mask = im.point(lambda i: i > 0 and 255) # use black as transparent
        source[A].paste(mask)
        im = Image.merge(im.mode, source)  # build a new multiband image

       self.graphic = ImageTk.PhotoImage(image=im)
       self.canvas.create_image(100, 100, image=self.graphic)
    if __name__ == "__main__":
     root = Tk()
     test = Transparency(root)
     root.mainloop()

It colors the canvas green, and produces a black background. An image is
merged with the background. I tried out the program. It executes, but I
do not see where the transparency is apparent. I used a gif with a
picture of a telescope on a white background, and the result is what I
would see if I pasted the telescope and white background onto the green
canvas.

If there's something missing in my observation, I'd like to know what it is.

-- 

           Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

             (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time)****

          "Less than all cannot satisfy Man." -- William Blake





More information about the Image-SIG mailing list