Hello I'm working on a little paint program. It basically draws circles on a tkinter Canvas when mouse is pressed and simultaneously draws circle on an PIL image at the same time (with same circle properties).
My problem is that with a too small circle radius, and when I move too fast to draw a chain of circles, I don't have the same tkinter output and PIL image output.
I'm using python 3.8.5 and PIL 7.0.0
#! /usr/bin/env python3
import tkinter as tk
from tkinter import *
from PIL import Image,ImageDraw,ImageTk
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
#Tkinter Classical Canvas:
self.canv= Canvas(self, bg="white", height=500,
width=500)
self.canv.pack()
#identic sized PIL image:
self.image1 = Image.new("RGB", (500, 500), "white")
self.draw = ImageDraw.Draw(self.image1)
self.buttons()
def buttons(self):
#size of the diameter parameter "button":
self.choose_diam_button =Scale(self, from_=1, to=100, orient=HORIZONTAL)
self.choose_diam_button.pack()
self.bouton_save = Button(self, text="save",font = "Ubuntu",
command=self.save)
self.bouton_save.pack()
self.setup()
def save(self):
self.chiffre_status=0
#the file is saved in an "IMG_DATA" gif file in directory
self.filename = "IMG_DATA"
self.image1.save(self.filename , format='GIF')
def reset(self, event):
self.old_x, self.old_y = None, None
def setup(self):
self.old_x = None
self.old_y = None
#boutton de taille du pinceau
self.diameter = self.choose_diam_button.get()
self.color = 'black'
self.canv.bind('<B1-Motion>', self.paint)
self.canv.bind('<ButtonRelease-1>', self.reset)
def paint(self,event):
self.diameter = self.choose_diam_button.get()
if self.old_x and self.old_y:
self.x=event.x+self.diameter
self.y=event.y+self.diameter
#The next line draws on the visible tkinter Canvas
self.canv.create_oval(self.old_x, self.old_y,self.x ,self.y ,
outline=self.color, fill=self.color)
#The next line draws on the PIL invisible image1
self.draw.ellipse([self.old_x, self.old_y, self.x, self.y],outline=self.color, fill=self.color)
self.old_x = event.x
self.old_y = event.y
appli = App()
appli.title("stack question")
appli.mainloop()
Tkinter visible output:
Saved "Parallel" PIL image: