Skip to content

Commit

Permalink
Merge branch 'main' into 152-improve-error-handling-of-table-transfor…
Browse files Browse the repository at this point in the history
…mers
  • Loading branch information
sibre28 authored Jun 23, 2023
2 parents ca53141 + c877530 commit f2a4a8e
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/safeds/data/image/containers/_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from PIL.Image import open as open_image

from safeds.data.image.typing import ImageFormat
from safeds.exceptions._data import WrongFileExtensionError


class Image:
Expand Down Expand Up @@ -416,3 +417,40 @@ def invert_colors(self) -> Image:
new_image = Image(data, self._format)
new_image._image = ImageOps.invert(new_image._image)
return new_image

def rotate_right(self) -> Image:
"""
Return the png-image clockwise rotated by 90 degrees.
Returns
-------
result : Image
The image clockwise rotated by 90 degrees.
"""
if self.format != ImageFormat.PNG:
raise WrongFileExtensionError("/image", ".png")

imagecopy = copy.deepcopy(self)
imagecopy._image = imagecopy._image.rotate(270, expand=True)
return imagecopy

def rotate_left(self) -> Image:
"""
Return the png-image counter-clockwise rotated by 90 degrees.
Returns
-------
result : Image
The image counter-clockwise rotated by 90 degrees.
Raises
------
WrongFileExtensionError
If given a File that's not PNG.
"""
if self.format != ImageFormat.PNG:
raise WrongFileExtensionError("/image", ".png")

imagecopy = copy.deepcopy(self)
imagecopy._image = imagecopy._image.rotate(90, expand=True)
return imagecopy
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.
39 changes: 39 additions & 0 deletions tests/safeds/data/image/containers/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from safeds.data.image.containers import Image
from safeds.data.image.typing import ImageFormat
from safeds.data.tabular.containers import Table
from safeds.exceptions._data import WrongFileExtensionError

from tests.helpers import resolve_resource_path

Expand Down Expand Up @@ -383,3 +384,41 @@ 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


class TestRotate:
def test_should_return_clockwise_rotated_image(
self,
) -> None:
image = Image.from_png_file(resolve_resource_path("image/snapshot_boxplot.png"))
image = image.rotate_right()
image2 = Image.from_png_file(resolve_resource_path("image/snapshot_boxplot_right_rotation.png"))
assert image == image2

def test_should_return_counter_clockwise_rotated_image(
self,
) -> None:
image = Image.from_png_file(resolve_resource_path("image/snapshot_boxplot.png"))
image = image.rotate_left()
image2 = Image.from_png_file(resolve_resource_path("image/snapshot_boxplot_left_rotation.png"))
assert image == image2

def test_should_raise_if_not_png_right(self) -> None:
with pytest.raises(
WrongFileExtensionError,
match=(
"The file /image has a wrong file extension. Please provide a file with the following extension\\(s\\):"
" .png"
),
):
Image.from_jpeg_file(resolve_resource_path("image/white_square.jpg")).rotate_right()

def test_should_raise_if_not_png_left(self) -> None:
with pytest.raises(
WrongFileExtensionError,
match=(
"The file /image has a wrong file extension. Please provide a file with the following extension\\(s\\):"
" .png"
),
):
Image.from_jpeg_file(resolve_resource_path("image/white_square.jpg")).rotate_left()

0 comments on commit f2a4a8e

Please sign in to comment.