PIL\Tkinter and Transparencies, Rubber Lines, and Dragging ImageObjects

Discussion in 'Python' started by W. eWatson, Apr 7, 2009.

  1. W. eWatson

    W. eWatson Guest

    Basically, I'd like to know how one (broadly, e.g., references in Win-land)
    does IP (image processing) and drawing techniques such as rubber lines, and
    dragging image objects across the canvas. I know there are some pretty
    powerful toolkits out there, but I'd like to limit this to PIL and Tkinter.
    If it can't be done with them, then I'll consider other possibilities. As a
    starter, on the topic of transparencies, 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.

    To further explore "drawing graphics", what roughly is the capability of
    Tkinter or PIL to allow one to place a transparent layer (mode, I guess in
    PIL may be roughly equivalent to a layer in tools like Photoshop) on top of
    an image and then move the transparency around over the image with a mouse?

    --
    W. eWatson

    (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
    Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet

    Web Page: <www.speckledwithstars.net/>
     
    W. eWatson, Apr 7, 2009
    #1
    1. Advertising

  2. W. eWatson

    W. eWatson Guest

    You got it. That lamda did look a little odd. The white background is opaque
    and the telescope is seen as green. The program will ask for a file. I
    didn't write the code.

    Eric Brunel wrote:
    > W. eWatson wrote:
    >> Basically, I'd like to know how one (broadly, e.g., references in Win-land)
    >> does IP (image processing) and drawing techniques such as rubber lines, and
    >> dragging image objects across the canvas. I know there are some pretty
    >> powerful toolkits out there, but I'd like to limit this to PIL and Tkinter.
    >> If it can't be done with them, then I'll consider other possibilities. As a
    >> starter, on the topic of transparencies, consider this program that I pulled
    >> off the web and was posted in 1999. It purports to illustrate how one might
    >> produce a transparency.

    >
    > OK, maybe I'm dumb but:
    >
    >> #!/usr/bin/python
    >> # see http://mail.python.org/pipermail/python-list/1999-May/003388.html
    >> from Tkinter import *
    >> import Image, ImageTk

    ....
    > HTH
    > - Eric -



    --
    W. eWatson

    (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
    Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet

    Web Page: <www.speckledwithstars.net/>
     
    W. eWatson, Apr 7, 2009
    #2
    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. Mike

    Rubber Band Box Over Tiff

    Mike, Dec 30, 2003, in forum: Java
    Replies:
    2
    Views:
    411
  2. Cinamon Thunder

    css transparencies

    Cinamon Thunder, Aug 22, 2006, in forum: HTML
    Replies:
    3
    Views:
    397
    dorayme
    Aug 23, 2006
  3. rfr
    Replies:
    0
    Views:
    654
  4. Sverre
    Replies:
    2
    Views:
    3,682
    Sverre
    Dec 17, 2009
  5. Jerry Hill
    Replies:
    0
    Views:
    493
    Jerry Hill
    Oct 20, 2010
Loading...

Share This Page