Skip to content

Commit 10b944c

Browse files
committed
Added type annotations to image_mobject.py
1 parent a50d836 commit 10b944c

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

manim/mobject/types/image_mobject.py

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@
1818
from ...constants import *
1919
from ...mobject.mobject import Mobject
2020
from ...utils.bezier import interpolate
21-
from ...utils.color import WHITE, ManimColor, color_to_int_rgb
21+
from ...utils.color import (
22+
WHITE,
23+
YELLOW_C,
24+
ManimColor,
25+
ParsableManimColor,
26+
color_to_int_rgb,
27+
)
2228
from ...utils.images import change_to_rgba_array, get_full_raster_image_path
2329

2430
__all__ = ["ImageMobject", "ImageMobjectFromCamera"]
@@ -61,9 +67,14 @@ def __init__(
6167
def get_pixel_array(self) -> PixelArray:
6268
raise NotImplementedError()
6369

64-
def set_color(self, color, alpha=None, family=True):
70+
def set_color( # type: ignore[override]
71+
self,
72+
color: ParsableManimColor = YELLOW_C,
73+
alpha: Any = None,
74+
family: bool = True,
75+
) -> AbstractImageMobject:
6576
# Likely to be implemented in subclasses, but no obligation
66-
pass
77+
raise NotImplementedError()
6778

6879
def set_resampling_algorithm(self, resampling_algorithm: int) -> Self:
6980
"""
@@ -209,18 +220,23 @@ def __init__(
209220
self.orig_alpha_pixel_array = self.pixel_array[:, :, 3].copy()
210221
super().__init__(scale_to_resolution, **kwargs)
211222

212-
def get_pixel_array(self):
223+
def get_pixel_array(self) -> PixelArray:
213224
"""A simple getter method."""
214225
return self.pixel_array
215226

216-
def set_color(self, color, alpha=None, family=True):
227+
def set_color( # type: ignore[override]
228+
self,
229+
color: ParsableManimColor = YELLOW_C,
230+
alpha: Any = None,
231+
family: bool = True,
232+
) -> Self:
217233
rgb = color_to_int_rgb(color)
218234
self.pixel_array[:, :, :3] = rgb
219235
if alpha is not None:
220236
self.pixel_array[:, :, 3] = int(255 * alpha)
221237
for submob in self.submobjects:
222238
submob.set_color(color, alpha, family)
223-
self.color = color
239+
self.color = ManimColor(color)
224240
return self
225241

226242
def set_opacity(self, alpha: float) -> Self:
@@ -252,7 +268,7 @@ def fade(self, darkness: float = 0.5, family: bool = True) -> Self:
252268
return self
253269

254270
def interpolate_color(
255-
self, mobject1: ImageMobject, mobject2: ImageMobject, alpha: float
271+
self, mobject1: Mobject, mobject2: Mobject, alpha: float
256272
) -> None:
257273
"""Interpolates the array of pixel color values from one ImageMobject
258274
into an array of equal size in the target ImageMobject.
@@ -268,6 +284,8 @@ def interpolate_color(
268284
alpha
269285
Used to track the lerp relationship. Not opacity related.
270286
"""
287+
assert isinstance(mobject1, ImageMobject)
288+
assert isinstance(mobject2, ImageMobject)
271289
assert mobject1.pixel_array.shape == mobject2.pixel_array.shape, (
272290
f"Mobject pixel array shapes incompatible for interpolation.\n"
273291
f"Mobject 1 ({mobject1}) : {mobject1.pixel_array.shape}\n"
@@ -291,7 +309,7 @@ def interpolate_color(
291309

292310
def get_style(self) -> dict[str, Any]:
293311
return {
294-
"fill_color": ManimColor(self.color.get_rgb()).to_hex(),
312+
"fill_color": ManimColor(self.color.to_rgb()).to_hex(),
295313
"fill_opacity": self.fill_opacity,
296314
}
297315

@@ -320,7 +338,7 @@ def __init__(
320338
super().__init__(scale_to_resolution=False, **kwargs)
321339

322340
# TODO: Get rid of this.
323-
def get_pixel_array(self):
341+
def get_pixel_array(self) -> PixelArray:
324342
self.pixel_array = self.camera.pixel_array
325343
return self.pixel_array
326344

@@ -331,7 +349,11 @@ def add_display_frame(self, **kwargs: Any) -> Self:
331349
self.add(self.display_frame)
332350
return self
333351

334-
def interpolate_color(self, mobject1, mobject2, alpha) -> None:
352+
def interpolate_color(
353+
self, mobject1: Mobject, mobject2: Mobject, alpha: float
354+
) -> None:
355+
assert isinstance(mobject1, ImageMobject)
356+
assert isinstance(mobject2, ImageMobject)
335357
assert mobject1.pixel_array.shape == mobject2.pixel_array.shape, (
336358
f"Mobject pixel array shapes incompatible for interpolation.\n"
337359
f"Mobject 1 ({mobject1}) : {mobject1.pixel_array.shape}\n"

mypy.ini

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,6 @@ ignore_errors = True
112112
[mypy-manim.mobject.three_d.three_dimensions]
113113
ignore_errors = True
114114

115-
[mypy-manim.mobject.types.image_mobject]
116-
ignore_errors = True
117-
118115
[mypy-manim.mobject.types.point_cloud_mobject]
119116
ignore_errors = True
120117

0 commit comments

Comments
 (0)