I need to reduce the latency in creating a circular gradient of arbitrary radius that's then mapped to the current mouse position. Though the project uses OpenGL for video-card interaction, alas, for legacy reasons, currently uses aggdraw for drawing (with Pillow wrapping PIL). However, PySDL2 runs the rest of the UI, so its drawing library is available in principle to the project.This function returns an aggdraw object which is elsewhere converted into a byte array with numpy and passed to OpenGL. I wrote this working, if rudimentary, function because I couldn't find a better one. But the latency between mouse motion and the gradient draw increases dramatically with radius value (see my code; should be obvious why). I'm very open to solutions that use the SDL2 drawing library instead but I've never used it and would appreciate being treated like the nub I am for such solutions. I know that's more of a stackoverflow request, but my enqueries there aren't getting much attention, leading me to believe that my solution isn't a egregiously wrong haha. I think it must be possible to do this faster than the screen refresh rate, there is  a latency tolerance of ~30ms (project's context is graduate work in the cognitive science of vision). I know this also depends on hardware; generally, we're talking iMacs no older than 3 years.Basically, I'm sure this can be done much better, but I am waaay out of my depth; I usually work with with web languages (JavaScript/PHP/HTML/CSS)!def mouse_gradient(radius):        mp = mouse_pos()  # custom function, just wraps fetching mouse coords from sdl2        mouse_grad = None        mouse_grad = aggdraw.Draw('RGBA', (radius, radius), (0, 0, 0, 0))        for n in range(radius):            n = float(n)            r = float(radius - n)            opacity = int(( 0.25 * n / radius) * 100)            brush = aggdraw.Brush((255, 0, 0), int(opacity))            tl = (radius - r) // 2  # ie. top-left            br = (radius - r) // 2 + r  # ie. bottom-right            mouse_grad.ellipse((tl, tl, br, br), brush)        return mouse_grad  # where it is subsequently blit to the screenNotes for the specific context if you want them:Since the code I've provided is so sparse, as a courtesy I should mention that this interfaces with a large module I've been writing for ~1yr (KLIBS). Said module primarily does a lot that's impertinent here, but, the below function does plug into lot of in-house code. Alas, I've yet to write documentation for the module because it's (currently) for a local context. This function is, verbatim, my code however; and the rest of the library has been well tested; the overhead is <10ms from anything else going on.
Optimizing a drawing a circular gradient in Python (aggdraw, PIL or SDL2)
python;performance;graphics