Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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))
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