Skip to content

Commit

Permalink
feat: added invert_colors method (#367)
Browse files Browse the repository at this point in the history
Closes #[282].

### Summary of Changes

added `invert_colors` method to `Image`
added tests


Co-authored-by: PhilipGutberlet
<92990487+PhilipGutberlet@users.noreply.github.com>

---------

Co-authored-by: PhilipGutberlet <92990487+PhilipGutberlet@users.noreply.github.com>
Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com>
  • Loading branch information
3 people authored Jun 16, 2023
1 parent 1312fe7 commit 1e4d110
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
23 changes: 22 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
from PIL import ImageFilter, ImageOps
from PIL.Image import Image as PillowImage
from PIL.Image import open as open_image

Expand Down Expand Up @@ -316,3 +316,24 @@ def blur(self, radius: int = 1) -> Image:
new_image = Image(data, self._format)
new_image._image = new_image._image.filter(ImageFilter.BoxBlur(radius))
return new_image

def invert_colors(self) -> Image:
"""
Return the image with inverted colors.
Returns
-------
result : Image
The image with inverted colors.
"""
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 = ImageOps.invert(new_image._image)
return new_image
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/original.jpg
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 @@ -265,6 +265,23 @@ def test_should_be_original(self) -> None:
assert image == image2


class TestInvertColors:
def test_should_invert_colors_png(self) -> None:
image = Image.from_png_file(resolve_resource_path("image/original.png"))
image = image.invert_colors()
image2 = Image.from_png_file(resolve_resource_path("image/inverted_colors_original.png"))
assert image == image2

def test_should_invert_colors_jpeg(self) -> None:
image = Image.from_jpeg_file(resolve_resource_path("image/original.jpg"))
image = image.invert_colors()
image.to_jpeg_file(resolve_resource_path("image/inverted_colors_original1.jpg"))
image = Image.from_jpeg_file(resolve_resource_path("image/inverted_colors_original1.jpg"))
image2 = Image.from_jpeg_file(resolve_resource_path("image/inverted_colors_original.jpg"))
assert image == image2
Path.unlink(Path(resolve_resource_path("image/inverted_colors_original1.jpg")))


class TestBlur:
def test_should_return_blurred_png_image(self) -> None:
image = Image.from_png_file(resolve_resource_path("image/boy.png"))
Expand Down

0 comments on commit 1e4d110

Please sign in to comment.