Skip to content

Commit

Permalink
feat: remove Image.__array__ (#964)
Browse files Browse the repository at this point in the history
### Summary of Changes

Remove the `Image.__array__` dunder method, which is used if an `Image`
object is passed to the `numpy.array` function. Reasons:

1. Integration with specific third-party libraries is out-of-scope so
far.
2. If we ever add this, a method `to_numpy_array` would be preferable,
since it's more visible and clearer. `__array__` could mean anything.
  • Loading branch information
lars-reimann authored Nov 26, 2024
1 parent e10e6bb commit d2f59c9
Showing 1 changed file with 0 additions and 20 deletions.
20 changes: 0 additions & 20 deletions src/safeds/data/image/containers/_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
from safeds.exceptions import IllegalFormatError

if TYPE_CHECKING:
from numpy import dtype, ndarray
from torch import Tensor


Expand Down Expand Up @@ -174,25 +173,6 @@ def __sizeof__(self) -> int:
"""
return sys.getsizeof(self._image_tensor) + self._image_tensor.element_size() * self._image_tensor.nelement()

def __array__(self, numpy_dtype: str | dtype | None = None) -> ndarray:
"""
Return the image as a numpy array.
Returns
-------
numpy_array:
The image as numpy array.
"""
from numpy import uint8

return (
self._image_tensor.permute(1, 2, 0)
.detach()
.cpu()
.numpy()
.astype(uint8 if numpy_dtype is None else numpy_dtype)
)

def _repr_jpeg_(self) -> bytes | None:
"""
Return a JPEG image as bytes.
Expand Down

0 comments on commit d2f59c9

Please sign in to comment.