Skip to content

Commit

Permalink
feat: Add find_edges method to Image class (#383)
Browse files Browse the repository at this point in the history
Closes #288

### Summary of Changes

Add method find_edges to Image that returns a new image containing the
edges but leaves the original unchanged.

<!-- Please provide a summary of changes in this pull request, ensuring
all changes are explained. -->

---------

Co-authored-by: patrikguempel <128832338+patrikguempel@users.noreply.github.com>
  • Loading branch information
alex-senger and patrikguempel committed Jun 23, 2023
1 parent 8fe40fa commit d14b6ce
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/safeds/data/image/containers/_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,3 +409,17 @@ def rotate_left(self) -> Image:
image_copy = copy.deepcopy(self)
image_copy._image = image_copy._image.rotate(90, expand=True)
return image_copy

def find_edges(self) -> Image:
"""
Return a grayscale image with the highlighted edges.
Returns
-------
result : Image
The image with edges found.
"""
image_copy = copy.deepcopy(self)
image_copy = image_copy.convert_to_grayscale()
image_copy._image = image_copy._image.filter(ImageFilter.FIND_EDGES)
return image_copy
Binary file added tests/resources/image/edgyBoy.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions tests/safeds/data/image/containers/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,3 +404,19 @@ def test_should_return_clockwise_rotated_image(self, image: Image, expected: Ima
def test_should_return_counter_clockwise_rotated_image(self, image: Image, expected: Image) -> None:
image = image.rotate_left()
assert image == expected


class TestFindEdges:
@pytest.mark.parametrize(
("image", "expected"),
[
(
Image.from_png_file(resolve_resource_path("image/boy.png")),
Image.from_png_file(resolve_resource_path("image/edgyBoy.png")),
),
],
ids=["find_edges"],
)
def test_should_return_edges_of_image(self, image: Image, expected: Image) -> None:
image = image.find_edges()
assert image == expected

0 comments on commit d14b6ce

Please sign in to comment.