Skip to content

Commit

Permalink
feat: sharpen image (#364)
Browse files Browse the repository at this point in the history
Closes #286.

### Summary of Changes
Added sharpening feature for images.

Co-authored-by: daniaHu <[email protected]>

---------

Co-authored-by: daniaHu <[email protected]>
Co-authored-by: megalinter-bot <[email protected]>
Co-authored-by: Alex Senger <[email protected]>
  • Loading branch information
4 people authored Jun 16, 2023
1 parent 1e4d110 commit 3444700
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/safeds/data/image/containers/_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import Any, BinaryIO

import PIL
from PIL import ImageFilter, ImageOps
from PIL import ImageEnhance, ImageFilter, ImageOps
from PIL.Image import Image as PillowImage
from PIL.Image import open as open_image

Expand Down Expand Up @@ -317,6 +317,24 @@ def blur(self, radius: int = 1) -> Image:
new_image._image = new_image._image.filter(ImageFilter.BoxBlur(radius))
return new_image

def sharpen(self, factor: float) -> Image:
"""
Return the sharpened image.
Parameters
----------
factor: The amount of sharpness to be applied to the image.
Factor 1.0 is considered to be neutral and does not make any changes.
Returns
-------
result : Image
The image sharpened by the given factor.
"""
image_copy = copy.deepcopy(self)
image_copy._image = ImageEnhance.Sharpness(image_copy._image).enhance(factor)
return image_copy

def invert_colors(self) -> Image:
"""
Return the image with inverted colors.
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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/sharpen/to_sharpen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions tests/safeds/data/image/containers/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,20 @@ def test_should_crop_png_image(self) -> None:
image = image.crop(0, 0, 100, 100)
image2 = Image.from_png_file(resolve_resource_path("image/whiteCropped.png"))
assert image == image2


class TestSharpen:
@pytest.mark.parametrize("factor", [-1, 0.5, 10])
def test_should_sharpen(self, factor: float) -> None:
image = Image.from_png_file(resolve_resource_path("image/sharpen/to_sharpen.png"))
image2 = image.sharpen(factor)
image2.to_png_file(resolve_resource_path("image/sharpen/sharpened_by_" + str(factor) + ".png"))
assert image != image2
assert image2 == Image.from_png_file(
resolve_resource_path("image/sharpen/sharpened_by_" + str(factor) + ".png"),
)

def test_should_not_sharpen(self) -> None:
image = Image.from_png_file(resolve_resource_path("image/sharpen/to_sharpen.png"))
image2 = image.sharpen(1)
assert image == image2

0 comments on commit 3444700

Please sign in to comment.