Skip to content

Commit

Permalink
the to_image method scales the arr to uint8 if otherwise
Browse files Browse the repository at this point in the history
  • Loading branch information
MAfarrag committed Jul 28, 2024
1 parent 00473f8 commit 04dce75
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
6 changes: 3 additions & 3 deletions cleopatra/array_glyph.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ def apply_colormap(self, cmap: Union[Colormap, str]) -> np.ndarray:
Parameters
----------
cmap: LinearSegmentedColormap
cmap: LinearSegmentedColormap/str
colormap.
Returns
Expand Down Expand Up @@ -597,8 +597,8 @@ def to_image(self) -> Image.Image:
<PIL.Image.Image image mode=RGB size=3x3 at 0x7F5E0D2F4C40>
"""
# This is done to scale the values between 0 and 255
# TODO: not sure if scaling the array to rgb is necessary here.
return Image.fromarray(self.scale_to_rgb()).convert("RGB")
arr = self.arr if self.arr.dtype == "uint8" else self.scale_to_rgb()
return Image.fromarray(arr).convert("RGB")

def scale_to_rgb(self) -> np.ndarray:
"""Create an RGB image.
Expand Down
18 changes: 13 additions & 5 deletions tests/test_array_glyph.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,12 +355,20 @@ def test_scale_to_rgb():
assert rgb_arr.dtype == "uint8"


def test_to_image():
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]])
my_glyph = ArrayGlyph(arr)
class TestToImage:
def test_int64_arr(self):
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]])
my_glyph = ArrayGlyph(arr)

image = my_glyph.to_image()
assert isinstance(image, Image.Image)

def test_uint_arr(self):
arr = np.random.randint(0, 255, size=(4, 4)).astype(np.uint8)
my_glyph = ArrayGlyph(arr)

image = my_glyph.to_image()
assert isinstance(image, Image.Image)
image = my_glyph.to_image()
assert isinstance(image, Image.Image)


def test_adjust_ticks():
Expand Down

0 comments on commit 04dce75

Please sign in to comment.