saving a tkinter canvas to gif

Reid Nichol rnichol_rrc at yahoo.com
Fri Apr 16 01:33:59 EDT 2004


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.sebastien at ouranos.ca (biner) wrote in message news:<b82e5469.0404141027.2ba8896d at posting.google.com>...
> 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.sebastien at ouranos.ca



More information about the Python-list mailing list