Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added crop() method in image and tests #365

Merged
merged 3 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading
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
Loading
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
Loading
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
Loading
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
Loading
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