How to draw an image, so that my image is used as a brush How to draw an image, so that my image is used as a brush tkinter tkinter

How to draw an image, so that my image is used as a brush


I GOT ITI did not know that the image that was drawn on the canvas, should be saved, so what I did is store the images in a matrix, which belongs to the canvas.Here is the code, just in case ...

from tkinter import *from PIL import Image, ImageTkmaster = Tk()w = Canvas(master, width=800, height=400)w.dib = [{} for k in range(10000)]w.pack(expand = YES, fill = BOTH)puntero = 0def paint( event ):    global w, P_img_crop, puntero    #I get the mouse coordinates    x, y = ( event.x - 1 ), ( event.y - 1 )    #I open and draw the image    img_crop = Image.open('C:/Users/Andres/Documents/PROYECTOS INCONCLUSOS/PAINT MATEW PYTHON/hola.png')    w.dib[puntero]['image'] = ImageTk.PhotoImage(img_crop)    w.create_image((x,y), anchor=NW, image=w.dib[puntero]['image'])    puntero += 1    if(puntero >=10000):        puntero = 0w.bind( "<B1-Motion>", paint )mainloop()


All you need to do remove the image creation inside the paint() function. Then you will achieve what you want because otherwise it creates the image again and doesn't save a copy behind. In other words, when you move the brush, the previous image is garbage collected.

Code:

from tkinter import *from PIL import Image, ImageTkmaster = Tk()w = Canvas(master, width=800, height=400)w.pack(expand = YES, fill = BOTH)img_crop = Image.open('yes.png')P_img_crop = ImageTk.PhotoImage(img_crop)def paint(event):    global P_img_crop    #I get the mouse coordinates    x, y = event.x - 1, event.y - 1    #I open and draw the image    w.create_image(x, y, image = P_img_crop)    w.bind("<B1-Motion>", paint)master.mainloop()

Hope this helps!