Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Account for dtype in the pixel array so the maximum value stays correct in the invert function (#3493)

Jérome Eertmans0a24cad22aee
diff --git a/manim/mobject/types/image_mobject.py b/manim/mobject/types/image_mobject.py
index cb0248d3a..990fb6768 100644
--- a/manim/mobject/types/image_mobject.py
+++ b/manim/mobject/types/image_mobject.py
@@ -191,7 +191,9 @@ class ImageMobject(AbstractImageMobject):
self.pixel_array, self.pixel_array_dtype
)
if self.invert:
- self.pixel_array[:, :, :3] = 255 - self.pixel_array[:, :, :3]
+ self.pixel_array[:, :, :3] = (
+ np.iinfo(self.pixel_array_dtype).max - self.pixel_array[:, :, :3]
+ )
super().__init__(scale_to_resolution, **kwargs)
def get_pixel_array(self):
diff --git a/tests/module/mobject/test_image.py b/tests/module/mobject/test_image.py
new file mode 100644
index 000000000..a79fce5b8
--- /dev/null
+++ b/tests/module/mobject/test_image.py
@@ -0,0 +1,14 @@
+import numpy as np
+import pytest
+
+from manim import ImageMobject
+
+
[email protected]("dtype", [np.uint8, np.uint16])
+def test_invert_image(dtype):
+ array = (255 * np.random.rand(10, 10, 4)).astype(dtype)
+ image = ImageMobject(array, pixel_array_dtype=dtype, invert=True)
+ assert image.pixel_array.dtype == dtype
+
+ array[:, :, :3] = np.iinfo(dtype).max - array[:, :, :3]
+ assert np.allclose(array, image.pixel_array)