A community-maintained Python framework for creating mathematical animations.
Implement `OpenGLImageMobject` (#2534)
icedcoffeeee7f5624ef6226
diff --git a/manim/mobject/opengl/opengl_image_mobject.py b/manim/mobject/opengl/opengl_image_mobject.pynew file mode 100644index 000000000..59727f2ea--- /dev/null+++ b/manim/mobject/opengl/opengl_image_mobject.py@@ -0,0 +1,80 @@+from __future__ import annotations++__all__ = [+ "OpenGLImageMobject",+]++from pathlib import Path++import numpy as np+from PIL import Image++from manim.mobject.opengl.opengl_surface import OpenGLSurface, OpenGLTexturedSurface+from manim.utils.images import get_full_raster_image_path+++class OpenGLImageMobject(OpenGLTexturedSurface):+ def __init__(+ self,+ filename_or_array: str | Path | np.ndarray,+ width: float = None,+ height: float = None,+ image_mode: str = "RGBA",+ resampling_algorithm: int = Image.BICUBIC,+ opacity: float = 1,+ gloss: float = 0,+ shadow: float = 0,+ **kwargs,+ ):+ self.image = filename_or_array+ self.resampling_algorithm = resampling_algorithm+ if type(filename_or_array) == np.ndarray:+ self.size = self.image.shape[1::-1]+ elif isinstance(filename_or_array, (str, Path)):+ path = get_full_raster_image_path(filename_or_array)+ self.size = Image.open(path).size++ if width is None and height is None:+ width = 4 * self.size[0] / self.size[1]+ height = 4+ if height is None:+ height = width * self.size[1] / self.size[0]+ if width is None:+ width = height * self.size[0] / self.size[1]++ surface = OpenGLSurface(+ lambda u, v: np.array([u, v, 0]),+ [-width / 2, width / 2],+ [-height / 2, height / 2],+ opacity=opacity,+ gloss=gloss,+ shadow=shadow,+ )++ super().__init__(+ surface,+ self.image,+ image_mode=image_mode,+ opacity=opacity,+ gloss=gloss,+ shadow=shadow,+ **kwargs,+ )++ def get_image_from_file(+ self,+ image_file: str | Path | np.ndarray,+ image_mode: str,+ ):+ 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(+ np.array(image_file.shape[:2])+ * 200, # assumption of 200 ppmu (pixels per manim unit) would suffice+ resample=self.resampling_algorithm,+ )+ )diff --git a/manim/mobject/opengl/opengl_surface.py b/manim/mobject/opengl/opengl_surface.pyindex 1d72d56a8..542baeea9 100644--- a/manim/mobject/opengl/opengl_surface.py+++ b/manim/mobject/opengl/opengl_surface.py@@ -1,14 +1,18 @@from __future__ import annotations+from pathlib import Path+from typing import Iterable+import modernglimport numpy as np+from PIL import Imagefrom manim.constants import *from manim.mobject.opengl.opengl_mobject import OpenGLMobjectfrom manim.utils.bezier import integer_interpolate, interpolatefrom manim.utils.color import *from manim.utils.config_ops import _Data, _Uniforms-from manim.utils.images import get_full_raster_image_path+from manim.utils.images import change_to_rgba_array, get_full_raster_image_pathfrom manim.utils.iterables import listifyfrom manim.utils.space_ops import normalize_along_axis@@ -324,22 +328,37 @@ class OpenGLTexturedSurface(OpenGLSurface):num_textures = _Uniforms()def __init__(- self, uv_surface, image_file, dark_image_file=None, shader_folder=None, **kwargs+ self,+ uv_surface: OpenGLSurface,+ image_file: str | Path,+ dark_image_file: str | Path = None,+ image_mode: str | Iterable[str] = "RGBA",+ shader_folder: str | Path = None,+ **kwargs,):self.uniforms = {}if not isinstance(uv_surface, OpenGLSurface):raise Exception("uv_surface must be of type OpenGLSurface")+ if type(image_file) == np.ndarray:+ image_file = change_to_rgba_array(image_file)+# Set texture information- if dark_image_file is None:- dark_image_file = image_file- self.num_textures = 1- else:- self.num_textures = 2+ if isinstance(image_mode, (str, Path)):+ image_mode = [image_mode] * 2+ image_mode_light, image_mode_dark = image_modetexture_paths = {- "LightTexture": get_full_raster_image_path(image_file),- "DarkTexture": get_full_raster_image_path(dark_image_file),+ "LightTexture": self.get_image_from_file(+ image_file,+ image_mode_light,+ ),+ "DarkTexture": self.get_image_from_file(+ dark_image_file or image_file,+ image_mode_dark,+ ),}+ if dark_image_file:+ self.num_textures = 2self.uv_surface = uv_surfaceself.uv_func = uv_surface.uv_func@@ -349,6 +368,14 @@ class OpenGLTexturedSurface(OpenGLSurface):self.gloss = self.uv_surface.glosssuper().__init__(texture_paths=texture_paths, **kwargs)+ def get_image_from_file(+ self,+ image_file: str | Path,+ image_mode: str,+ ):+ image_file = get_full_raster_image_path(image_file)+ return Image.open(image_file).convert(image_mode)+def init_data(self):super().init_data()self.im_coords = np.zeros((0, 2))diff --git a/manim/mobject/types/image_mobject.py b/manim/mobject/types/image_mobject.pyindex 10b61a27a..241a7e3fa 100644--- a/manim/mobject/types/image_mobject.py+++ b/manim/mobject/types/image_mobject.py@@ -17,7 +17,7 @@ from ...constants import *from ...mobject.mobject import Mobjectfrom ...utils.bezier import interpolatefrom ...utils.color import WHITE, color_to_int_rgb-from ...utils.images import get_full_raster_image_path+from ...utils.images import change_to_rgba_array, get_full_raster_image_pathclass AbstractImageMobject(Mobject):@@ -183,26 +183,13 @@ class ImageMobject(AbstractImageMobject):else:self.pixel_array = np.array(filename_or_array)self.pixel_array_dtype = kwargs.get("pixel_array_dtype", "uint8")- self.change_to_rgba_array()+ self.pixel_array = change_to_rgba_array(+ self.pixel_array, self.pixel_array_dtype+ )if self.invert:self.pixel_array[:, :, :3] = 255 - self.pixel_array[:, :, :3]super().__init__(scale_to_resolution, **kwargs)- def change_to_rgba_array(self):- """Converts an RGB array into RGBA with the alpha value opacity maxed."""- pa = self.pixel_array- if len(pa.shape) == 2:- pa = pa.reshape(list(pa.shape) + [1])- if pa.shape[2] == 1:- pa = pa.repeat(3, axis=2)- if pa.shape[2] == 3:- alphas = 255 * np.ones(- list(pa.shape[:2]) + [1],- dtype=self.pixel_array_dtype,- )- pa = np.append(pa, alphas, axis=2)- self.pixel_array = pa-def get_pixel_array(self):"""A simple getter method."""return self.pixel_arraydiff --git a/manim/opengl/__init__.py b/manim/opengl/__init__.pyindex 85d583086..83f6a4ccd 100644--- a/manim/opengl/__init__.py+++ b/manim/opengl/__init__.py@@ -7,6 +7,7 @@ except ImportError:from manim.mobject.opengl.dot_cloud import *+from manim.mobject.opengl.opengl_image_mobject import *from manim.mobject.opengl.opengl_mobject import *from manim.mobject.opengl.opengl_point_cloud_mobject import *from manim.mobject.opengl.opengl_surface import *diff --git a/manim/renderer/opengl_renderer.py b/manim/renderer/opengl_renderer.pyindex 5f368d900..ffd29cc1b 100644--- a/manim/renderer/opengl_renderer.py+++ b/manim/renderer/opengl_renderer.py@@ -381,18 +381,21 @@ class OpenGLRenderer:mesh.render()def get_texture_id(self, path):- if path not in self.path_to_texture_id:- # A way to increase tid's sequentially+ if repr(path) not in self.path_to_texture_id:tid = len(self.path_to_texture_id)- im = Image.open(path)texture = self.context.texture(- size=im.size,- components=len(im.getbands()),- data=im.tobytes(),+ size=path.size,+ components=len(path.getbands()),+ data=path.tobytes(),)+ texture.repeat_x = False+ texture.repeat_y = False+ texture.filter = (moderngl.NEAREST, moderngl.NEAREST)+ texture.swizzle = "RRR1" if path.mode == "L" else "RGBA"texture.use(location=tid)- self.path_to_texture_id[path] = tid- return self.path_to_texture_id[path]+ self.path_to_texture_id[repr(path)] = tid++ return self.path_to_texture_id[repr(path)]def update_skipping_status(self):"""diff --git a/manim/utils/images.py b/manim/utils/images.pyindex 544533f89..a8ef93e93 100644--- a/manim/utils/images.py+++ b/manim/utils/images.py@@ -2,7 +2,12 @@from __future__ import annotations-__all__ = ["get_full_raster_image_path", "drag_pixels", "invert_image"]+__all__ = [+ "get_full_raster_image_path",+ "drag_pixels",+ "invert_image",+ "change_to_rgba_array",+]import numpy as npfrom PIL import Image@@ -32,3 +37,19 @@ def invert_image(image: np.array) -> Image:arr = np.array(image)arr = (255 * np.ones(arr.shape)).astype(arr.dtype) - arrreturn Image.fromarray(arr)+++def change_to_rgba_array(image, dtype="uint8"):+ """Converts an RGB array into RGBA with the alpha value opacity maxed."""+ pa = image+ if len(pa.shape) == 2:+ pa = pa.reshape(list(pa.shape) + [1])+ if pa.shape[2] == 1:+ pa = pa.repeat(3, axis=2)+ if pa.shape[2] == 3:+ alphas = 255 * np.ones(+ list(pa.shape[:2]) + [1],+ dtype=dtype,+ )+ pa = np.append(pa, alphas, axis=2)+ return pa