I am making an app that makes an animation of expanding circles. See the example code where one circle draws almost instantly, while the next one takes orders of magnitude longer. What is causing this and how can I fix it?
from PIL import Image,ImageDraw
import time
im = Image.new("RGB",(1920,1080))
dr = ImageDraw.Draw(im)
x = -11215.155812019575
y = -37218.4732436535
radius = 38932.697140522076
coords = (x-radius,y-radius,x+radius,y+radius)
color = (218, 185, 237)
weight = 3
t = time.time()
dr.ellipse(coords,fill="black",outline=color,width=weight)
tt = time.time()
elapsed = tt - t
print("ellipse 1 time: "+str(round(elapsed,3))+"sn")
ellipse 1 time: 0.003s
im.save("test1.png")
im = Image.new("RGB",(1920,1080))
dr = ImageDraw.Draw(im)
x = -11823.913602620554
y = -39106.396905836176
radius = 40879.33199754818
coords = (x-radius,y-radius,x+radius,y+radius)
color = (218, 185, 237)
weight = 3
t = time.time()
dr.ellipse(coords,fill="black",outline=color,width=weight)
tt = time.time()
elapsed = tt - t
print("ellipse 2 time: "+str(round(elapsed,3))+"sn")
ellipse 2 time: 44.444s
Checking the pillow docs, I find that the ellipse()
function wraps around a function called draw_ellipse()
which I can’t seem to find.
def ellipse(self, xy: Coords, fill=None, outline=None, width=1) -> None:
"""Draw an ellipse."""
ink, fill = self._getink(outline, fill)
if fill is not None:
self.draw.draw_ellipse(xy, fill, 1)
if ink is not None and ink != fill and width != 0:
self.draw.draw_ellipse(xy, ink, 0, width)
I suspect the problem may be related to the size of the ellipse exceeding some limit, but I’m not sure how it can be that enlarging a circle by 5% can cause the time to draw it to grow 1500000%.
1