Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Fix `OpenGLImageMobject` converting image to string and calculation of enlargement size (#5081)

Benjamin Hacklee21a1ead477
diff --git a/manim/mobject/opengl/opengl_image_mobject.py b/manim/mobject/opengl/opengl_image_mobject.py
index d7617d3ff..181cfc5a9 100644
--- a/manim/mobject/opengl/opengl_image_mobject.py
+++ b/manim/mobject/opengl/opengl_image_mobject.py
@@ -19,6 +19,27 @@ if TYPE_CHECKING:
__all__ = ["OpenGLImageMobject"]
+# The OpenGL renderer samples textures with nearest-neighbor filtering. To apply
+# ``resampling_algorithm`` to image arrays anyway, they are upscaled with it
+# before they are uploaded as textures.
+
+# Maximum factor by which an image array is upscaled.
+_MAX_UPSCALE_FACTOR = 200
+# Maximum length (in pixels) of the longer side of an upscaled image array.
+_MAX_UPSCALED_SIDE_LENGTH = 4096
+
+
+def _get_upscale_factor(width: int, height: int) -> int:
+ """Return the factor by which an image array of the given size is upscaled.
+
+ The factor is at most ``_MAX_UPSCALE_FACTOR``, and the longer side of the
+ upscaled image is at most ``_MAX_UPSCALED_SIDE_LENGTH`` pixels. Image arrays
+ are never downscaled.
+ """
+ return max(
+ 1, min(_MAX_UPSCALE_FACTOR, _MAX_UPSCALED_SIDE_LENGTH // max(width, height))
+ )
+
class OpenGLImageMobject(OpenGLTexturedSurface):
def __init__(
@@ -76,12 +97,10 @@ class OpenGLImageMobject(OpenGLTexturedSurface):
if isinstance(image_file, (str, Path)):
return super().get_image_from_file(image_file, image_mode)
else:
- return (
- Image.fromarray(image_file.astype("uint8"))
- .convert(image_mode)
- .resize(
- image_file.shape[:2]
- * 200, # assumption of 200 ppmu (pixels per manim unit) would suffice
- resample=self.resampling_algorithm,
- )
+ image = Image.fromarray(image_file.astype("uint8")).convert(image_mode)
+ width, height = image.size
+ scale = _get_upscale_factor(width, height)
+ return image.resize(
+ (width * scale, height * scale),
+ resample=self.resampling_algorithm,
)
diff --git a/manim/renderer/opengl_renderer.py b/manim/renderer/opengl_renderer.py
index 21f90f9df..e356962ae 100644
--- a/manim/renderer/opengl_renderer.py
+++ b/manim/renderer/opengl_renderer.py
@@ -47,6 +47,7 @@ from .vectorized_mobject_rendering import (
if TYPE_CHECKING:
from collections.abc import Iterable
+ from pathlib import Path
from typing import Self
from manim._config.render_session import RenderSessionSpec
@@ -528,7 +529,8 @@ class OpenGLRenderer:
num_plays : float
The number of animation plays executed.
path_to_texture_id : dict[str, int]
- Mapping from texture file paths to OpenGL texture IDs.
+ Mapping from texture keys (file paths or image identifiers) to OpenGL
+ texture IDs.
pressed_keys : set[int]
Set of currently pressed key codes.
skip_animations : bool
@@ -571,6 +573,8 @@ class OpenGLRenderer:
self.pressed_keys: set[int] = set()
self.window: Window | None = None
self.path_to_texture_id: dict[str, int] = {}
+ # In-memory images that textures were created from, by texture key.
+ self._texture_images: dict[str, Image.Image] = {}
self.background_color = config["background_color"]
def init_scene(
@@ -723,7 +727,7 @@ class OpenGLRenderer:
# Set textures.
for name, path in shader_wrapper.texture_paths.items():
- tid = self.get_texture_id(str(path))
+ tid = self.get_texture_id(path)
shader_texture = shader.shader_program[name]
if not isinstance(
shader_texture, (moderngl.Uniform, moderngl.UniformBlock)
@@ -768,44 +772,52 @@ class OpenGLRenderer:
mesh.set_uniforms(self)
mesh.render()
- def get_texture_id(self, path: str) -> int:
+ def get_texture_id(self, path: str | Path | Image.Image) -> int:
"""
- Retrieves the OpenGL texture ID associated with the given image file path.
+ Retrieves the OpenGL texture ID associated with the given texture source.
- Automatically creates a new texture it it has not been loaded before.
+ Automatically creates a new texture if it has not been loaded before.
Parameters
----------
- path : str
- The file path to the texture image.
+ path
+ The file path to the texture image, or an already loaded image
+ (as used by :class:`.OpenGLTexturedSurface`).
Returns
-------
int
- The OpenGL texture ID corresponding to the given path.
+ The OpenGL texture ID corresponding to the given source.
"""
- return (
- self.path_to_texture_id[path]
- if path in self.path_to_texture_id
- else self._create_texture(path)
- )
+ key = f"<image {id(path)}>" if isinstance(path, Image.Image) else str(path)
+
+ if key not in self.path_to_texture_id:
+ self._create_texture(path, key)
+ return self.path_to_texture_id[key]
- def _create_texture(self, image_path: str) -> int:
+ def _create_texture(self, source: str | Path | Image.Image, key: str) -> int:
"""
- Create an OpenGL texture from the given image file path, get its texture ID,
- and store it in `self.path_to_texture_id[image_path]`.
+ Create an OpenGL texture from the given image or image file path, get its
+ texture ID, and store it in `self.path_to_texture_id[key]`.
Parameters
----------
- image_path : str
- The file path to the image to be loaded as a texture.
+ source
+ The image, or the file path to the image, to be loaded as a texture.
+ key
+ The key under which the texture ID is stored.
Returns
-------
int
The texture ID assigned to the newly created texture.
"""
- with Image.open(image_path) as img:
+ image_context: contextlib.AbstractContextManager[Image.Image] = (
+ contextlib.nullcontext(source)
+ if isinstance(source, Image.Image)
+ else Image.open(source)
+ )
+ with image_context as img:
tid = len(self.path_to_texture_id)
# grayscale image
@@ -828,7 +840,11 @@ class OpenGLRenderer:
texture.filter = (moderngl.NEAREST, moderngl.NEAREST)
texture.swizzle = swizzle
texture.use(location=tid)
- self.path_to_texture_id[image_path] = tid
+ self.path_to_texture_id[key] = tid
+ if isinstance(source, Image.Image):
+ # Keep the image alive, so that its id (part of the key) cannot be
+ # reused by another image while the texture exists.
+ self._texture_images[key] = source
return tid
def update_skipping_status(self) -> None:
diff --git a/tests/opengl/test_image_mobject_opengl.py b/tests/opengl/test_image_mobject_opengl.py
new file mode 100644
index 000000000..11c0121a7
--- /dev/null
+++ b/tests/opengl/test_image_mobject_opengl.py
@@ -0,0 +1,60 @@
+from __future__ import annotations
+
+import numpy as np
+from PIL import Image
+
+from manim import Scene
+from manim.mobject.opengl.opengl_image_mobject import OpenGLImageMobject
+
+RED = np.array([255, 0, 0, 255])
+BLUE = np.array([0, 0, 255, 255])
+
+
+def _red_over_blue(height: int, width: int) -> np.ndarray:
+ pixels = np.zeros((height, width, 3), dtype=np.uint8)
+ pixels[: height // 2, :, 0] = 255
+ pixels[height // 2 :, :, 2] = 255
+ return pixels
+
+
+def _render_and_sample(image: OpenGLImageMobject) -> tuple[np.ndarray, np.ndarray]:
+ """Render ``image`` (with height 4, i.e. half the frame height) and return
+ the pixel colors in the centers of its upper and lower halves.
+ """
+ scene = Scene()
+ scene.add(image)
+ scene.renderer.update_frame(scene)
+ frame = scene.renderer.get_frame()
+ height, width, _ = frame.shape
+ upper = frame[int(height * 3 / 8), width // 2]
+ lower = frame[int(height * 5 / 8), width // 2]
+ return upper, lower
+
+
+def test_opengl_image_mobject_from_file(using_opengl_renderer, tmp_path):
+ path = tmp_path / "red_over_blue.png"
+ Image.fromarray(_red_over_blue(64, 64)).save(path)
+
+ upper, lower = _render_and_sample(OpenGLImageMobject(path, height=4))
+
+ np.testing.assert_array_equal(upper, RED)
+ np.testing.assert_array_equal(lower, BLUE)
+
+
+def test_opengl_image_mobject_from_array(using_opengl_renderer):
+ upper, lower = _render_and_sample(
+ OpenGLImageMobject(_red_over_blue(4, 6), height=4)
+ )
+
+ np.testing.assert_array_equal(upper, RED)
+ np.testing.assert_array_equal(lower, BLUE)
+
+
+def test_opengl_image_mobject_array_upscaling(using_opengl_renderer):
+ small = OpenGLImageMobject(_red_over_blue(4, 6))
+ texture = small.get_image_from_file(_red_over_blue(4, 6), "RGBA")
+ assert texture.size == (6 * 200, 4 * 200)
+
+ large = OpenGLImageMobject(_red_over_blue(1000, 1500))
+ texture = large.get_image_from_file(_red_over_blue(1000, 1500), "RGBA")
+ assert texture.size == (1500 * 2, 1000 * 2)