From 28eaf28227e7723d95f986c72d0448622b12e645 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 9 Apr 2026 11:36:01 +0000 Subject: [PATCH] Optimize cairo surface to numpy array conversion Replaced slow advanced indexing in `cairo_surface_to_numpy` with an efficient `copy()` and slice assignment to swap the Red and Blue channels. This significantly reduces the rendering time bottleneck. Co-authored-by: OEvortex <158988478+OEvortex@users.noreply.github.com> --- src/handanim/core/utils.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/handanim/core/utils.py b/src/handanim/core/utils.py index 6055c53..5db527a 100644 --- a/src/handanim/core/utils.py +++ b/src/handanim/core/utils.py @@ -72,7 +72,10 @@ def cairo_surface_to_numpy(surface: cairo.ImageSurface): h = surface.get_height() a = np.ndarray(shape=(h, w, 4), dtype=np.uint8, buffer=buf) # Cairo is ARGB, convert to a contiguous RGBA buffer for imageio. - return np.ascontiguousarray(a[:, :, [2, 1, 0, 3]]) + b = a.copy() + b[:, :, 0] = a[:, :, 2] + b[:, :, 2] = a[:, :, 0] + return b def solve_quad_eqn(