Dear all, what can be the most efficiency way of converting the canvas to a bitmap (what ever type, gif png, jpg...) image? Right now we are saving to postscript and then using the PIL library convert it to bitmap. Thanks in advance Vasilis
Hi, On Mon, 23 Oct 2017 12:07:20 +0000 Vasilis Vlachoudis <Vasilis.Vlachoudis@cern.ch> wrote:
Dear all,
what can be the most efficiency way of converting the canvas to a bitmap (what ever type, gif png, jpg...) image? Right now we are saving to postscript and then using the PIL library convert it to bitmap.
you can use the tkImg library to do that. tkImg's "window" image type allows you to create "screenshots" of Tk widgets on the fly. To load the "window" image type, do: somewidget.tk.call('package', 'require', 'img::window') Or to load all available image formats: somewidget.tk.call('package', 'require', 'Img') Then you should be able to load the canvas as image and export the image to a gif or png file (or any other format your tkImg install supports) with something like: img = Tkinter.PhotoImage(format='window', data=canvas._w) img.write('canvas.png', format='png') del img tkImg can be a bit tricky though. I have had problem with the window image type when using Tkinter's default widget names. From my experience it seems a good idea to create the widget in question and all its ancestors with the widget name explicitely set, as in f0 = Frame(parent, name='f0') Loading all image types at once also often causes problems (resp. crashes), so I recommend to explicitely only load the image types you really need. Best regards Michael .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. When a child is taught ... its programmed with simple instructions -- and at some point, if its mind develops properly, it exceeds the sum of what it was taught, thinks independently. -- Dr. Richard Daystrom, "The Ultimate Computer", stardate 4731.3.
On Mon, 23 Oct 2017 18:53:24 +0200 Michael Lange <klappnase@web.de> wrote:
you can use the tkImg library to do that. tkImg's "window" image type allows you to create "screenshots" of Tk widgets on the fly.
Oh, and I forgot to mention, with the window image type it is necessary that the canvas is *completely* visible on the screen, otherwise you might end up with blacked-out image parts or even a TclError. So for a real big canvas that requires scrolling I am afraid this is no option. Best regards Michael .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. War isn't a good life, but it's life. -- Kirk, "A Private Little War", stardate 4211.8
Thank you very much! It is a good option to explore. Thanks Vasilis ________________________________________ From: Tkinter-discuss [tkinter-discuss-bounces+vasilis.vlachoudis=cern.ch@python.org] on behalf of Michael Lange [klappnase@web.de] Sent: Monday, October 23, 2017 19:07 To: tkinter-discuss@python.org Subject: Re: [Tkinter-discuss] Convert Canvas to bitmap image On Mon, 23 Oct 2017 18:53:24 +0200 Michael Lange <klappnase@web.de> wrote:
you can use the tkImg library to do that. tkImg's "window" image type allows you to create "screenshots" of Tk widgets on the fly.
Oh, and I forgot to mention, with the window image type it is necessary that the canvas is *completely* visible on the screen, otherwise you might end up with blacked-out image parts or even a TclError. So for a real big canvas that requires scrolling I am afraid this is no option. Best regards Michael .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. War isn't a good life, but it's life. -- Kirk, "A Private Little War", stardate 4211.8 _______________________________________________ Tkinter-discuss mailing list Tkinter-discuss@python.org https://mail.python.org/mailman/listinfo/tkinter-discuss
participants (2)
-
Michael Lange -
Vasilis Vlachoudis