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: blur Image method #363

Merged
merged 13 commits into from
Jun 16, 2023
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 @@ -6,6 +6,7 @@
from typing import Any, BinaryIO

import PIL
from PIL import ImageFilter
from PIL.Image import Image as PillowImage
from PIL.Image import open as open_image

Expand Down Expand Up @@ -274,3 +275,30 @@ def flip_horizontally(self) -> Image:
imagecopy = copy.deepcopy(self)
imagecopy._image = self._image.transpose(PIL.Image.FLIP_LEFT_RIGHT)
return imagecopy

def blur(self, radius: int = 1) -> Image:
"""
Return the blurred image.

Parameters
----------
radius : int
Radius is directly proportional to the blur value. The radius is equal to the amount of pixels united in each direction.
A Radius of 1 will result in a united box of 9 pixels.

Returns
-------
result : Image
The blurred image
"""
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.filter(ImageFilter.BoxBlur(radius))
PhilipGutberlet marked this conversation as resolved.
Show resolved Hide resolved
return new_image
Binary file added tests/resources/image/blurredboy.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/blurredboy.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/boy.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/boy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions tests/safeds/data/image/containers/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,26 @@ def test_should_be_original(self) -> None:
assert image == image2


class TestBlur:
def test_should_return_blurred_png_image(self) -> None:
image = Image.from_png_file(resolve_resource_path("image/boy.png"))
image = image.blur(2)
image.to_png_file(resolve_resource_path("image/blurredboy1.png"))
image = Image.from_png_file(resolve_resource_path("image/blurredboy1.png"))
image2 = Image.from_png_file(resolve_resource_path("image/blurredboy.png"))
assert image._image == image2._image

def test_should_return_blurred_jpg_image(self) -> None:
image = Image.from_jpeg_file(resolve_resource_path("image/boy.jpg"))
image = image.blur(2)
image.to_jpeg_file(resolve_resource_path("image/blurredboy1.jpg"))
image = Image.from_jpeg_file(resolve_resource_path("image/blurredboy1.jpg"))
image2 = Image.from_jpeg_file(resolve_resource_path("image/blurredboy.jpg"))
assert image._image == image2._image
Path.unlink(Path(resolve_resource_path("image/blurredboy1.jpg")))
Path.unlink(Path(resolve_resource_path("image/blurredboy1.png")))


class TestCrop:
def test_should_crop_jpg_image(self) -> None:
image = Image.from_jpeg_file(resolve_resource_path("image/white.jpg"))
Expand Down