diff --git a/src/safeds/data/image/containers/_image.py b/src/safeds/data/image/containers/_image.py index 2c3659859..39f26f194 100644 --- a/src/safeds/data/image/containers/_image.py +++ b/src/safeds/data/image/containers/_image.py @@ -221,6 +221,34 @@ def resize(self, new_width: int, new_height: int) -> Image: new_image._image = new_image._image.resize((new_width, new_height)) return new_image + def crop(self, x: int, y: int, width: int, height: int) -> Image: + """ + Return an image that has been cropped to a given bounding rectangle. + + Parameters + ---------- + x: the x coordinate of the top-left corner of the bounding rectangle + y: the y coordinate of the top-left corner of the bounding rectangle + width: the width of the bounding rectangle + height: the height of the bounding rectangle + + Returns + ------- + result : Image + The image with the + """ + data = io.BytesIO() + repr_png = self._repr_png_() + repr_jpeg = self._repr_jpeg_() + if repr_png is not None: + data = io.BytesIO(repr_png) + elif repr_jpeg is not None: + data = io.BytesIO(repr_jpeg) + + new_image = Image(data, self._format) + new_image._image = new_image._image.crop((x, y, (x + width), (y + height))) + return new_image + def flip_vertically(self) -> Image: """ Flip the image vertically (horizontal axis, flips up-down and vice versa). diff --git a/tests/resources/image/white.jpg b/tests/resources/image/white.jpg new file mode 100644 index 000000000..b15a32d05 Binary files /dev/null and b/tests/resources/image/white.jpg differ diff --git a/tests/resources/image/white.png b/tests/resources/image/white.png new file mode 100644 index 000000000..4849863e8 Binary files /dev/null and b/tests/resources/image/white.png differ diff --git a/tests/resources/image/whiteCropped.jpg b/tests/resources/image/whiteCropped.jpg new file mode 100644 index 000000000..e80f49303 Binary files /dev/null and b/tests/resources/image/whiteCropped.jpg differ diff --git a/tests/resources/image/whiteCropped.png b/tests/resources/image/whiteCropped.png new file mode 100644 index 000000000..fb18be863 Binary files /dev/null and b/tests/resources/image/whiteCropped.png differ diff --git a/tests/resources/white.jpg/white.png b/tests/resources/white.jpg/white.png new file mode 100644 index 000000000..4849863e8 Binary files /dev/null and b/tests/resources/white.jpg/white.png differ diff --git a/tests/safeds/data/image/containers/test_image.py b/tests/safeds/data/image/containers/test_image.py index 0b8e09bd6..781e48672 100644 --- a/tests/safeds/data/image/containers/test_image.py +++ b/tests/safeds/data/image/containers/test_image.py @@ -247,3 +247,17 @@ def test_should_be_original(self) -> None: image = Image.from_png_file(resolve_resource_path("image/original.png")) image2 = image.flip_horizontally().flip_horizontally() assert image == image2 + + +class TestCrop: + def test_should_crop_jpg_image(self) -> None: + image = Image.from_jpeg_file(resolve_resource_path("image/white.jpg")) + image = image.crop(0, 0, 100, 100) + image2 = Image.from_jpeg_file(resolve_resource_path("image/whiteCropped.jpg")) + assert image == image2 + + def test_should_crop_png_image(self) -> None: + image = Image.from_png_file(resolve_resource_path("image/white.png")) + image = image.crop(0, 0, 100, 100) + image2 = Image.from_png_file(resolve_resource_path("image/whiteCropped.png")) + assert image == image2