diff --git a/src/safeds/data/image/containers/_image.py b/src/safeds/data/image/containers/_image.py index c5f9c2b31..38cf88c4c 100644 --- a/src/safeds/data/image/containers/_image.py +++ b/src/safeds/data/image/containers/_image.py @@ -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 @@ -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. diff --git a/tests/resources/image/sharpen/sharpened_by_-1.png b/tests/resources/image/sharpen/sharpened_by_-1.png new file mode 100644 index 000000000..927b96838 Binary files /dev/null and b/tests/resources/image/sharpen/sharpened_by_-1.png differ diff --git a/tests/resources/image/sharpen/sharpened_by_0.5.png b/tests/resources/image/sharpen/sharpened_by_0.5.png new file mode 100644 index 000000000..afe187a9e Binary files /dev/null and b/tests/resources/image/sharpen/sharpened_by_0.5.png differ diff --git a/tests/resources/image/sharpen/sharpened_by_10.png b/tests/resources/image/sharpen/sharpened_by_10.png new file mode 100644 index 000000000..c7590fc53 Binary files /dev/null and b/tests/resources/image/sharpen/sharpened_by_10.png differ diff --git a/tests/resources/image/sharpen/to_sharpen.png b/tests/resources/image/sharpen/to_sharpen.png new file mode 100644 index 000000000..2c9e5d18e Binary files /dev/null and b/tests/resources/image/sharpen/to_sharpen.png differ diff --git a/tests/safeds/data/image/containers/test_image.py b/tests/safeds/data/image/containers/test_image.py index 19ca708f1..968a50d07 100644 --- a/tests/safeds/data/image/containers/test_image.py +++ b/tests/safeds/data/image/containers/test_image.py @@ -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