Skip to content

ImageSize

A container for image size data.

Parameters:

Name Type Description Default
width int

the width of the image

required
height int

the height of the image

required
channel int

the channel of the image

required

Raises:

Type Description
OutOfBoundsError:

if width or height are below 1

ValueError

if an invalid channel is given

Source code in src/safeds/data/image/typing/_image_size.py
class ImageSize:
    """
    A container for image size data.

    Parameters
    ----------
    width:
        the width of the image
    height:
        the height of the image
    channel:
        the channel of the image

    Raises
    ------
    OutOfBoundsError:
        if width or height are below 1
    ValueError
        if an invalid channel is given
    """

    def __init__(self, width: int, height: int, channel: int, *, _ignore_invalid_channel: bool = False) -> None:
        if width < 1 or height < 1:
            raise OutOfBoundsError(min(width, height), lower_bound=ClosedBound(1))
        elif not _ignore_invalid_channel and channel not in (1, 3, 4):
            raise ValueError(f"Channel {channel} is not a valid channel option. Use either 1, 3 or 4")
        elif channel < 1:
            raise OutOfBoundsError(channel, name="channel", lower_bound=ClosedBound(1))
        self._width = width
        self._height = height
        self._channel = channel

    @staticmethod
    def from_image(image: Image) -> ImageSize:
        """
        Create a `ImageSize` of a given image.

        Parameters
        ----------
        image:
            the given image for the `ImageSize`

        Returns
        -------
        image_size:
            the calculated `ImageSize`
        """
        return ImageSize(image.width, image.height, image.channel)

    def __eq__(self, other: object) -> bool:
        if not isinstance(other, ImageSize):
            return NotImplemented
        return (self is other) or (
            self._width == other._width and self._height == other._height and self._channel == other._channel
        )

    def __hash__(self) -> int:
        return _structural_hash(self._width, self._height, self._channel)

    def __sizeof__(self) -> int:
        return sys.getsizeof(self._width) + sys.getsizeof(self._height) + sys.getsizeof(self._channel)

    def __str__(self) -> str:
        return f"{self._width}x{self._height}x{self._channel} (WxHxC)"

    @property
    def width(self) -> int:
        """
        Get the width of this `ImageSize` in pixels.

        Returns
        -------
        width:
            The width of this `ImageSize`.
        """
        return self._width

    @property
    def height(self) -> int:
        """
        Get the height of this `ImageSize` in pixels.

        Returns
        -------
        height:
            The height of this `ImageSize`.
        """
        return self._height

    @property
    def channel(self) -> int:
        """
        Get the channel of this `ImageSize` in pixels.

        Returns
        -------
        channel:
            The channel of this `ImageSize`.
        """
        return self._channel

channel: int property

Get the channel of this ImageSize in pixels.

Returns:

Name Type Description
channel int

The channel of this ImageSize.

height: int property

Get the height of this ImageSize in pixels.

Returns:

Name Type Description
height int

The height of this ImageSize.

width: int property

Get the width of this ImageSize in pixels.

Returns:

Name Type Description
width int

The width of this ImageSize.

from_image(image) staticmethod

Create a ImageSize of a given image.

Parameters:

Name Type Description Default
image Image

the given image for the ImageSize

required

Returns:

Name Type Description
image_size ImageSize

the calculated ImageSize

Source code in src/safeds/data/image/typing/_image_size.py
@staticmethod
def from_image(image: Image) -> ImageSize:
    """
    Create a `ImageSize` of a given image.

    Parameters
    ----------
    image:
        the given image for the `ImageSize`

    Returns
    -------
    image_size:
        the calculated `ImageSize`
    """
    return ImageSize(image.width, image.height, image.channel)