Serial Save#
This is probably the easiest method to directly save in a useful format. As stated in the normal save method if no output file is given, then postscript generates a string. PIL can interpret the output once made into byte data. That said nothing will be saved unless the postscript data is delayed until the canvas image is fully generated, hence the use of the button to start the saving.
The image serial saved by PIL after reading the postscript output#
Show/Hide Code save_io.py
from tkinter import Tk, Canvas, Button
from PIL import Image
import io
root = Tk()
w = 60
h = 60
def save():
ps = cv.postscript(colormode='color')
img = Image.open(io.BytesIO(ps.encode('utf-8')))
img.save('../../images/canvas/save_io.jpg', 'jpeg')
cv = Canvas(root, width=w, height=h, bg='white')
cv.pack()
b= Button(root, text='save', command=save)
b.pack()
# nothing to see unless rectangle is black and canvas has bg
# make sure the save is delayed
cv.create_rectangle(10,10,50,50, outline='black')
root.mainloop()
Check the size of the output images/canvas/save_io.jpg it will be 59x59 but the original canvas was sized 60x60. Although pretty foolproof (I managed to do it) some checks on sizing need to be made, see the next side.