Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Add type annotations to ```opengl_image_mobject.py``` (#4536)

F. Muenkelab17eb58a3f2
diff --git a/manim/mobject/opengl/opengl_image_mobject.py b/manim/mobject/opengl/opengl_image_mobject.py
index 84e3ef7ad..d7617d3ff 100644
--- a/manim/mobject/opengl/opengl_image_mobject.py
+++ b/manim/mobject/opengl/opengl_image_mobject.py
@@ -5,6 +5,7 @@ __all__ = [
]
from pathlib import Path
+from typing import TYPE_CHECKING, Any
import numpy as np
from PIL import Image
@@ -13,26 +14,29 @@ from PIL.Image import Resampling
from manim.mobject.opengl.opengl_surface import OpenGLSurface, OpenGLTexturedSurface
from manim.utils.images import get_full_raster_image_path
+if TYPE_CHECKING:
+ import numpy.typing as npt
+
__all__ = ["OpenGLImageMobject"]
class OpenGLImageMobject(OpenGLTexturedSurface):
def __init__(
self,
- filename_or_array: str | Path | np.ndarray,
- width: float = None,
- height: float = None,
+ filename_or_array: str | Path | npt.NDArray,
+ width: float | None = None,
+ height: float | None = None,
image_mode: str = "RGBA",
- resampling_algorithm: int = Resampling.BICUBIC,
+ resampling_algorithm: Resampling = Resampling.BICUBIC,
opacity: float = 1,
gloss: float = 0,
shadow: float = 0,
- **kwargs,
+ **kwargs: Any,
):
self.image = filename_or_array
self.resampling_algorithm = resampling_algorithm
if isinstance(filename_or_array, np.ndarray):
- self.size = self.image.shape[1::-1]
+ self.size = filename_or_array.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
@@ -68,7 +72,7 @@ class OpenGLImageMobject(OpenGLTexturedSurface):
self,
image_file: str | Path | np.ndarray,
image_mode: str,
- ):
+ ) -> Image.Image:
if isinstance(image_file, (str, Path)):
return super().get_image_from_file(image_file, image_mode)
else:
@@ -76,7 +80,7 @@ class OpenGLImageMobject(OpenGLTexturedSurface):
Image.fromarray(image_file.astype("uint8"))
.convert(image_mode)
.resize(
- np.array(image_file.shape[:2])
+ 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.py
index e6fdeee45..27084635c 100644
--- a/manim/mobject/opengl/opengl_surface.py
+++ b/manim/mobject/opengl/opengl_surface.py
@@ -2,6 +2,7 @@ from __future__ import annotations
from collections.abc import Iterable
from pathlib import Path
+from typing import TYPE_CHECKING
import moderngl
import numpy as np
@@ -9,7 +10,6 @@ from PIL import Image
from manim.constants import *
from manim.mobject.opengl.opengl_mobject import OpenGLMobject
-from manim.typing import Point3D_Array, Vector3D_Array
from manim.utils.bezier import integer_interpolate, interpolate
from manim.utils.color import *
from manim.utils.config_ops import _Data, _Uniforms
@@ -17,6 +17,11 @@ from manim.utils.images import change_to_rgba_array, get_full_raster_image_path
from manim.utils.iterables import listify
from manim.utils.space_ops import normalize_along_axis
+if TYPE_CHECKING:
+ import numpy.typing as npt
+
+ from manim.typing import Point3D_Array, Vector3D_Array
+
__all__ = ["OpenGLSurface", "OpenGLTexturedSurface"]
@@ -83,7 +88,7 @@ class OpenGLSurface(OpenGLMobject):
render_primitive=moderngl.TRIANGLES,
depth_test=True,
shader_folder=None,
- **kwargs,
+ **kwargs: Any,
):
self.passed_uv_func = uv_func
self.u_range = u_range if u_range is not None else (0, 1)
@@ -374,7 +379,7 @@ class OpenGLTexturedSurface(OpenGLSurface):
def __init__(
self,
uv_surface: OpenGLSurface,
- image_file: str | Path,
+ image_file: str | Path | npt.NDArray,
dark_image_file: str | Path = None,
image_mode: str | Iterable[str] = "RGBA",
shader_folder: str | Path = None,
@@ -416,7 +421,7 @@ class OpenGLTexturedSurface(OpenGLSurface):
self,
image_file: str | Path,
image_mode: str,
- ):
+ ) -> Image.Image:
image_file = get_full_raster_image_path(image_file)
return Image.open(image_file).convert(image_mode)
diff --git a/mypy.ini b/mypy.ini
index 6cad4b91b..1a1c30542 100644
--- a/mypy.ini
+++ b/mypy.ini
@@ -88,9 +88,6 @@ ignore_errors = True
[mypy-manim.mobject.mobject]
ignore_errors = True
-[mypy-manim.mobject.opengl.opengl_image_mobject]
-ignore_errors = True
-
[mypy-manim.mobject.opengl.opengl_point_cloud_mobject]
ignore_errors = True