[Image-SIG] Re: HOW ? Snapshot Tk canvas as image
Fredrik Lundh
fredrik@pythonware.com
Tue May 27 16:36:56 EDT 2003
Graham Swallow wrote:
> How can I get an image from a Tk canvas.
> (other than rewrite or Ghostscript).
> The following limits are OK (as in screen grab)
>
> image is exact size of VISIBLE PART of canvas
> canvas is update-idle-tasks and not obscured
the following script works on Windows (requires PIL 1.1.4):
import sys
import Tkinter
import ImageGrab
canvas = Tkinter.Canvas(width=600, height=400)
canvas.create_line(0, 0, 600, 400, fill="red", width=10)
canvas.pack()
canvas.update()
if sys.platform == "win32":
# get window location
x0 = canvas.winfo_rootx()
y0 = canvas.winfo_rooty()
x1 = x0 + canvas.winfo_width()
y1 = y0 + canvas.winfo_height()
im = ImageGrab.grab((x0, y0, x1, y1))
else:
raise NotYetImplemented("oops!")
im.show()
under X windows, you could probably emulate the grab call by calling
"xwd -id %s" % canvas.winfo_id() and piping the output through xwd-
topnm. something like this might work:
elif os.name == "posix":
os.system("xwd -id %s | xwdtopnm >grab.ppm" % canvas.winfo_id())
im = Image.open("grab.ppm")
# (for production code, replace grab.ppm with a temporary filename,
# and make sure to remove it when done)
</F>
More information about the Image-SIG
mailing list