saving a tkinter canvas to gif

Discussion in 'Python' started by biner, Apr 14, 2004.

  1. biner

    biner Guest

    Hello,

    Is there any way to save a canvas created with tkinter to a gif (or
    any other graphic) without using PIL (I cannot build it on our unix
    machine)? For example with this simple code :
    >>>

    from Tkinter import *

    root=Tk()
    base=Canvas(root,width=50,height=50)
    base.create_rectangle(0,0,50,25,fill='red')
    base.create_rectangle(0,25,50,50,fill='blue')

    base.pack()
    root.mainloop()
    <<<
    How can I save the image with the two colored rectangle onto a graphic
    file?
    I looked on google and in the documentation but I didn't find how to
    do it. Thanks for any help.

    Sebastien.
     
    biner, Apr 14, 2004
    #1
    1. Advertising

  2. On Wed, 14 Apr 2004, biner wrote:

    > Is there any way to save a canvas created with tkinter to a gif (or
    > any other graphic) without using PIL (I cannot build it on our unix
    > machine)?


    Have you tried the postscript method of Tkinter.Canvas?

    > base=Canvas(root,width=50,height=50)
    > base.create_rectangle(0,0,50,25,fill='red')
    > base.create_rectangle(0,25,50,50,fill='blue')
    > base.pack()


    base.postscript("file.ps")

    /Mickel

    --
    Mickel Grönroos, application specialist, linguistics, Research support, CSC
    PL 405 (Tekniikantie 15 a D), 02101 Espoo, Finland, phone +358-9-4572237
    CSC is the Finnish IT center for science, www.csc.fi
     
    =?ISO-8859-1?Q?Mickel_Gr=F6nroos?=, Apr 15, 2004
    #2
    1. Advertising

  3. biner

    Reid Nichol Guest

    I've been dealing with the same problem. This was my solution for
    saving as jpeg. You can look up how to specify gif.


    # save the canvas as a ps file
    self.canvas.postscript(file='tmp.ps', x=start_x, y=start_y, height=h,
    width=w)

    # convert the ps to a jpeg - it'll be in the middle
    # of the pic
    # gs = ghostscript
    os.system('gs -sDEVICE=jpeg -sOutputFile=tmp.jpeg -dNOPAUSE -q -dBATCH
    tmp.ps')

    # extract the frame from that jpeg
    # use PIL here
    im = Image.open('tmp.jpeg')
    box = ((im.size[0] - self.movie_size[0])/2, (im.size[1] -
    self.movie_size[1])/2, (im.size[0] + self.movie_size[0])/2,
    (im.size[1] + s
    elf.movie_size[1])/2)
    to_save = im.crop(box)

    # save that extracted frame
    to_save.save('ultimate_text.jpeg', 'jpeg')


    (biner) wrote in message news:<>...
    > Hello,
    >
    > Is there any way to save a canvas created with tkinter to a gif (or
    > any other graphic) without using PIL (I cannot build it on our unix
    > machine)? For example with this simple code :
    > >>>

    > from Tkinter import *
    >
    > root=Tk()
    > base=Canvas(root,width=50,height=50)
    > base.create_rectangle(0,0,50,25,fill='red')
    > base.create_rectangle(0,25,50,50,fill='blue')
    >
    > base.pack()
    > root.mainloop()
    > <<<
    > How can I save the image with the two colored rectangle onto a graphic
    > file?
    > I looked on google and in the documentation but I didn't find how to
    > do it. Thanks for any help.
    >
    > Sebastien.
    >
     
    Reid Nichol, Apr 16, 2004
    #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. =?ISO-8859-1?Q?Mickel_Gr=F6nroos?=

    Re: saving a tkinter canvas

    =?ISO-8859-1?Q?Mickel_Gr=F6nroos?=, Apr 1, 2004, in forum: Python
    Replies:
    0
    Views:
    405
    =?ISO-8859-1?Q?Mickel_Gr=F6nroos?=
    Apr 1, 2004
  2. Askari
    Replies:
    2
    Views:
    712
    Askari
    Aug 30, 2004
  3. PhilC
    Replies:
    2
    Views:
    891
    PhilC
    Oct 25, 2004
  4. Andy Cho
    Replies:
    4
    Views:
    364
    Roedy Green
    Aug 19, 2009
  5. Replies:
    10
    Views:
    265
    Rick Johnson
    Mar 15, 2013
Loading...

Share This Page