0

image 1:

enter image description here

image 2:

enter image description here

I want to turn a tkinter canvas shape into an image. How do I do it?

Python Code

class Bricks:
    def __init__(self, canvas, color):
        self.canvas = canvas
        self.id = canvas.create_oval(5, 5, 25, 25, fill=color, width=2)

Source enter link description here

toyota Supra
  • 3,181
  • 4
  • 15
  • 19
i7-12700H
  • 1
  • 1

1 Answers1

1

See that thread to save an image from the canvas. Just make a smaller canvas, draw jour circle and save it.

from tkinter import *
from PIL import ImageGrab

def getter(widget):
    x=root.winfo_rootx()+widget.winfo_x()
    y=root.winfo_rooty()+widget.winfo_y()
    x1=x+widget.winfo_width()
    y1=y+widget.winfo_height()
    ImageGrab.grab().crop((x,y,x1,y1)).save("file path here")

root = Tk()
cv = Canvas(root)
cv.create_rectangle(10,10,50,50)
cv.pack()
root.mainloop()

See PhotoImage to load an image from a file.

from tkinter import PhotoImage

PhotoImage(file="path to image file")

Use canvas.create_image(x_pos, y_pos, image=some photoimage) to draw it.

from tkinter import PhotoImage

image = PhotoImage(file="path to image file")

root = Tk()
cv = Canvas(root)
cv.create_image(10,10, image=image)
cv.pack()
root.mainloop()
Sisyffe
  • 162
  • 11