Skip to content

Commit

Permalink
feat: added color adjustment feature (#409)
Browse files Browse the repository at this point in the history
Closes #380

### Summary of Changes
Added the color adjustment feature analogous to the previous image
tasks.

---------

Co-authored-by: megalinter-bot <[email protected]>
  • Loading branch information
patrikguempel and megalinter-bot authored Jul 7, 2023
1 parent dab6419 commit 2cbee36
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/safeds/data/image/containers/_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,36 @@ def adjust_contrast(self, factor: float) -> Image:
image_copy._image = ImageEnhance.Contrast(image_copy._image).enhance(factor)
return image_copy

def adjust_color_balance(self, factor: float) -> Image:
"""
Adjust the image's color balance.
Parameters
----------
factor: float
If factor > 1, increase color balance of image.
If factor = 1, no changes will be made.
If factor < 1, make image greyer.
Has to be bigger than or equal to 0.
Returns
-------
image: Image
The new, adjusted image.
"""
if factor < 0:
raise ValueError("Color factor has to be 0 or bigger.")
elif factor == 1:
warnings.warn(
"Color adjustment factor is 1.0, this will not make changes to the image.",
UserWarning,
stacklevel=2,
)

image_copy = copy.deepcopy(self)
image_copy._image = ImageEnhance.Color(image_copy._image).enhance(factor)
return image_copy

def blur(self, radius: int) -> Image:
"""
Return the blurred image.
Expand Down
Binary file added tests/resources/image/adjusted_colors/by_0.5.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/adjusted_colors/by_0.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/adjusted_colors/by_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
59 changes: 59 additions & 0 deletions tests/safeds/data/image/containers/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,65 @@ def test_should_invert_colors(self, image: Image, expected: Image) -> None:
assert image == expected


class TestColorAdjust:
@pytest.mark.parametrize(
("image", "factor", "expected"),
[
(
Image.from_png_file(resolve_resource_path("image/original.png")),
2,
Image.from_png_file(resolve_resource_path("image/adjusted_colors/by_2.png")),
),
(
Image.from_png_file(resolve_resource_path("image/original.png")),
0.5,
Image.from_png_file(resolve_resource_path("image/adjusted_colors/by_0.5.png")),
),
(
Image.from_png_file(resolve_resource_path("image/original.png")),
0,
Image.from_png_file(resolve_resource_path("image/adjusted_colors/by_0.png")),
),
],
ids=["add color", "remove color", "remove all color"],
)
def test_should_adjust_colors(self, image: Image, factor: float, expected: Image) -> None:
image = image.adjust_color_balance(factor)
assert image == expected

@pytest.mark.parametrize(
("image", "factor"),
[
(
Image.from_png_file(resolve_resource_path("image/original.png")),
-1,
),
],
ids=["negative"],
)
def test_should_throw(self, image: Image, factor: float) -> None:
with pytest.raises(ValueError, match="Color factor has to be 0 or bigger."):
image.adjust_color_balance(factor)

@pytest.mark.parametrize(
("image", "factor"),
[
(
Image.from_png_file(resolve_resource_path("image/original.png")),
1,
),
],
ids=["no change"],
)
def test_should_warn(self, image: Image, factor: float) -> None:
with pytest.warns(
UserWarning,
match="Color adjustment factor is 1.0, this will not make changes to the image.",
):
adjust = image.adjust_color_balance(factor)
assert adjust == image


class TestBlur:
@pytest.mark.parametrize(
("image", "expected"),
Expand Down

0 comments on commit 2cbee36

Please sign in to comment.