Skip to content

Commit

Permalink
feat: added crop() method in image and tests (#365)
Browse files Browse the repository at this point in the history
Closes #[284].

### Summary of Changes

Added `crop()` method in `Image`
Added tests


Co-authored-by: PhilipGutberlet
<92990487+PhilipGutberlet@users.noreply.github.com>

---------

Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com>
Co-authored-by: Philip Gutberlet <92990487+PhilipGutberlet@users.noreply.github.com>
  • Loading branch information
3 people committed Jun 16, 2023
1 parent 66f5f64 commit eba8163
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/safeds/data/image/containers/_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
Binary file added tests/resources/image/white.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/resources/image/white.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/resources/image/whiteCropped.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/resources/image/whiteCropped.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/resources/white.jpg/white.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions tests/safeds/data/image/containers/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit eba8163

Please sign in to comment.