Skip to content

Commit

Permalink
feat: rotate_left and rotate_right added to Image (#361)
Browse files Browse the repository at this point in the history
Closes #281.

### Summary of Changes

Added two methods to `Image`:
* `rotate_right` to rotate the image clockwise by 90 degrees
* `rotate_left` to rotate the image counter-clockwise by 90 degrees.

Both return a new image and leave the original unchanged.

---------

Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com>
  • Loading branch information
robmeth and megalinter-bot authored Jun 21, 2023
1 parent 1752feb commit c877530
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 c877530

Please sign in to comment.