Skip to content

Commit 0a24cad

Browse files
jeertmanspre-commit-ci[bot]behackl
authored
Account for dtype in the pixel array so the maximum value stays correct in the invert function (#3493)
* fix(lib): fix This fixes an issue where the `invert` argument would only work for `uint8` dtypes. Now the `max` value is updated according to the pixel array dtype. Maybe we should add unit tests for that, but haven't found an obvious place to put unit tests. * chore(ci): add basic test * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix(ci): wrong attr name * Update tests/module/mobject/test_image.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Benjamin Hackl <[email protected]>
1 parent 912ae76 commit 0a24cad

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

manim/mobject/types/image_mobject.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,9 @@ def __init__(
191191
self.pixel_array, self.pixel_array_dtype
192192
)
193193
if self.invert:
194-
self.pixel_array[:, :, :3] = 255 - self.pixel_array[:, :, :3]
194+
self.pixel_array[:, :, :3] = (
195+
np.iinfo(self.pixel_array_dtype).max - self.pixel_array[:, :, :3]
196+
)
195197
super().__init__(scale_to_resolution, **kwargs)
196198

197199
def get_pixel_array(self):

tests/module/mobject/test_image.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import numpy as np
2+
import pytest
3+
4+
from manim import ImageMobject
5+
6+
7+
@pytest.mark.parametrize("dtype", [np.uint8, np.uint16])
8+
def test_invert_image(dtype):
9+
array = (255 * np.random.rand(10, 10, 4)).astype(dtype)
10+
image = ImageMobject(array, pixel_array_dtype=dtype, invert=True)
11+
assert image.pixel_array.dtype == dtype
12+
13+
array[:, :, :3] = np.iinfo(dtype).max - array[:, :, :3]
14+
assert np.allclose(array, image.pixel_array)

0 commit comments

Comments
 (0)