Skip to content

Commit

Permalink
Account for dtype in the pixel array so the maximum value stays corre…
Browse files Browse the repository at this point in the history
…ct in the invert function (ManimCommunity#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]>
  • Loading branch information
3 people authored Dec 12, 2023
1 parent 912ae76 commit 0a24cad
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
4 changes: 3 additions & 1 deletion manim/mobject/types/image_mobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ def __init__(
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):
Expand Down
14 changes: 14 additions & 0 deletions tests/module/mobject/test_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import numpy as np
import pytest

from manim import ImageMobject


@pytest.mark.parametrize("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)

0 comments on commit 0a24cad

Please sign in to comment.