Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added color adjustment feature #409

Merged
merged 10 commits into from
Jul 7, 2023
14 changes: 14 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,20 @@ 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:
patrikguempel marked this conversation as resolved.
Show resolved Hide resolved
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
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 @@ -327,6 +327,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