Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions manim/mobject/opengl/opengl_image_mobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
]

from pathlib import Path
from typing import TYPE_CHECKING, Any

import numpy as np
from PIL import Image
Expand All @@ -13,26 +14,29 @@
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
Expand Down Expand Up @@ -68,15 +72,15 @@ def get_image_from_file(
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:
return (
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,
)
Expand Down
13 changes: 9 additions & 4 deletions manim/mobject/opengl/opengl_surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,26 @@

from collections.abc import Iterable
from pathlib import Path
from typing import TYPE_CHECKING

import moderngl
import numpy as np
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
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"]


Expand Down Expand Up @@ -83,7 +88,7 @@ def __init__(
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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -416,7 +421,7 @@ def get_image_from_file(
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)

Expand Down
3 changes: 0 additions & 3 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down