[Tutor] Can you create an image from Tkinter?

Alfred Milgrom fredm@smartypantsco.com
Wed Jun 18 22:53:01 2003


Just wanted to let other people on the list know the results of how to save=
=20
a Tkinter canvas image.

Magnus recommended looking at PIL, so I had another very careful read of=20
the manual.
Included in PIL is a module called ImageGrab which allows you to grab the=20
entire screen (or a defined section of it), or to get images from the=20
clipboard. This works only on Windows platforms.
(Note that ImageGrab is part of the PIL package but is not a function of=
 Image)

Also Fredrik Lundh was incredibly helpful in helping me sort out some=20
problems and also forwarded a link to one of his posts on how to grab just=
=20
the Tkinter canvas:

http://mail.python.org/pipermail/image-sig/2003-May/002292.html

The code example in that post is as follows:

the following script works on Windows (requires PIL 1.1.4):
import sys
import Tkinter
import ImageGrab

canvas =3D Tkinter.Canvas(width=3D600, height=3D400)
canvas.create_line(0, 0, 600, 400, fill=3D"red", width=3D10)
canvas.pack()

canvas.update()

if sys.platform =3D=3D "win32":

         # get window location
         x0 =3D canvas.winfo_rootx()
         y0 =3D canvas.winfo_rooty()
         x1 =3D x0 + canvas.winfo_width()
         y1 =3D y0 + canvas.winfo_height()

         im =3D ImageGrab.grab((x0, y0, x1, y1))

else:

         raise NotYetImplemented("oops!")

im.show()

Thanks again to everyone on the list for making this such an interesting=20
place to get and exchange information.
Thanks also to Fredrik Lundh for writing such great code and supporting it=
=20
so generously.

Fred Milgrom


At 09:14 PM 13/06/03 +0200, Magnus Lyck=E5 wrote:
>At 11:48 2003-06-13 +1000, Alfred Milgrom wrote:
>>I wonder if anyone can help me with creating savable images (say GIF or=20
>>BMP) under Python?
>
>I think you should take a look at PIL:
>http://www.pythonware.com/products/pil/index.htm#pil114
>
>A slightly old handbook is here:
>http://www.pythonware.com/library/pil/handbook/index.htm
>
>
>--
>Magnus Lycka (It's really Lyckå), magnus@thinkware.se
>Thinkware AB, Sweden, www.thinkware.se
>I code Python ~ The Agile Programming Language