I'm trying to 'export' a tkinter
canvas to a png
file, as in this answer: https://stackoverflow.com/a/55385764/15862653
I created a sample canvas and a button that triggers a function to export it to png:
import tkinter as tk
from PIL import Image
def save_as_png(canvas, fileName):
canvas.postscript(file='./' + fileName + '.eps', colormode='color')
img = Image.open(fileName + '.eps')
img.convert()
img.save(fileName + '.png', 'png')
root = tk.Tk()
canvas = tk.Canvas(root, width=100, height=100)
text = canvas.create_text(50, 50, anchor='center', text='Some text')
button = tk.Button(root, text='Save canvas', command=lambda: save_as_png(canvas, 'my canvas'))
canvas.pack()
button.pack()
root.mainloop()
The image that results has extremely low resolution, though. I took me a while even to understand that 'Some text' is actually what is written:
I tried fiddling with the arguments (dpi
, quality
, compression_level
) of both the open
and the save
methods, with no luck.