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: sharpen image #364

Merged
merged 19 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
2830d66
completed titanic tutorial in getting_started.ipynb
daniaHu Jun 16, 2023
0b104ee
Merge remote-tracking branch 'origin/286-sharpen-image' into 286-shar…
patrikguempel Jun 16, 2023
1e8a976
added sharpening feature
patrikguempel Jun 16, 2023
a8ef0a6
added sharpening feature
patrikguempel Jun 16, 2023
cf34c19
Merge remote-tracking branch 'origin/286-sharpen-image' into 286-shar…
patrikguempel Jun 16, 2023
f4a5401
style: apply automated linter fixes
megalinter-bot Jun 16, 2023
39d4727
used smaller images
patrikguempel Jun 16, 2023
1b25a22
Merge remote-tracking branch 'origin/286-sharpen-image' into 286-shar…
patrikguempel Jun 16, 2023
f677729
Update tests/safeds/data/image/containers/test_image.py
patrikguempel Jun 16, 2023
c1a37d3
added further tests
patrikguempel Jun 16, 2023
1429ea7
Merge remote-tracking branch 'origin/286-sharpen-image' into 286-shar…
patrikguempel Jun 16, 2023
cf279a2
Merge remote-tracking branch 'origin/286-sharpen-image' into 286-shar…
patrikguempel Jun 16, 2023
ce5efff
Merge remote-tracking branch 'origin/286-sharpen-image' into 286-shar…
patrikguempel Jun 16, 2023
12e518b
Merge branch 'main' of https://github.com/Safe-DS/Stdlib into 286-sha…
patrikguempel Jun 16, 2023
ff5527e
style: apply automated linter fixes
megalinter-bot Jun 16, 2023
6695c23
edited test
patrikguempel Jun 16, 2023
6a4749f
Merge branch 'main' of https://github.com/Safe-DS/Stdlib into 286-sha…
patrikguempel Jun 16, 2023
0e06cc4
Merge remote-tracking branch 'origin/286-sharpen-image' into 286-shar…
patrikguempel Jun 16, 2023
0277126
style: apply automated linter fixes
megalinter-bot Jun 16, 2023
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
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