Hello,
I would like to save the drawings I have made on a Canvas with the Tkinter Module.
Is it possible to make a .jpg or .gif or .bmp from a Tkinter window ?
Thank you!
Hello,
I would like to save the drawings I have made on a Canvas with the Tkinter Module.
Is it possible to make a .jpg or .gif or .bmp from a Tkinter window ?
Thank you!
A concise summary and practical options, building on ’s discovery of Canvas.postscript and ImageGrab and ’s suggestion to use an image viewer/screenshot tool.
The two reliable approaches are (A) export the canvas as PostScript and convert that to a bitmap, or (B) render into a Pillow image as you draw so you can save directly. PostScript conversion is simple and platform‑independent, but it requires a PostScript rasterizer (Ghostscript or ImageMagick) for conversion and it will not include Tkinter PhotoImage objects. Before calling postscript, make sure the canvas is fully laid out (call canvas.update() or canvas.update_idletasks()).
Example: save PostScript then open with Pillow (Ghostscript needed to read PS/EPS):
canvas.postscript(file='canvas.ps', colormode='color',
width=canvas.winfo_width(),
height=canvas.winfo_height())
from PIL import Image
img = Image.open('canvas.ps') # requires Ghostscript available to Pillow
img = img.convert('RGB')
img.save('canvas.jpg', 'JPEG', quality=90) If you need exact pixel-level control or you use embedded images on the canvas, mirror drawing commands into a Pillow ImageDraw object while you draw on the Tk canvas. That guarantees identical output and avoids external converters:
from PIL import Image, ImageDraw
w, h = canvas.winfo_width(), canvas.winfo_height()
img = Image.new('RGB', (w, h), 'white')
draw = ImageDraw.Draw(img)
# when drawing:
canvas.create_line(x1,y1,x2,y2, fill='black', width=2)
draw.line((x1,y1,x2,y2), fill='black', width=2)
img.save('export.jpg', 'JPEG', quality=90) Troubleshooting notes: PostScript exports use 72 DPI by default (sizes may need scaling), fonts in PS may differ from screen fonts, and PhotoImage content won’t appear in PS — use the mirroring approach to include raster images. On Windows the ImageGrab approach mentioned earlier is the quickest throwaway method; for cross‑platform or repeatable results, prefer postscript+conversion or direct Pillow rendering.
Ok i've found the response on www.developpez.net
It uses the .postscript method from canvas and the ImageGrab module
Bonjour,
Il y a la méthode '.postscript', qui génère ... du postscript.
Sous MS windows, tu peux utiliser ImageGrab (PIL). Je ne crois pas que cela fonctionne sous Linux.
x = canvas.winfo_rootx()
y = canvas.winfo_rooty()
w = canvas.winfo_width()
h = canvas.winfo_height()
img= ImageGrab.grab((x+2, y+2, x+w-2, y+h-2)).save("tmp.bmp")
It would be simple to use the capture option that comes with many image viewers like XnView (nice and free).
For another potential solution look at:
http://www.daniweb.com/code/snippet705.html
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.