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: Add adjust_color_balance method in Image #530

Merged
merged 3 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions src/safeds/data/image/containers/_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
if TYPE_CHECKING:
from torch.types import Device
import torchvision

# Disables torchvision V2 beta warnings
# Disabled because of RUFF Linter E402 (Module level import not at top of file)
# torchvision.disable_beta_transforms_warning()
from torchvision.transforms.v2 import PILToTensor
from torchvision.transforms.v2 import functional as func2
from torchvision.utils import save_image
Expand Down Expand Up @@ -434,6 +430,38 @@ def adjust_contrast(self, factor: float) -> Image:
else:
return Image(func2.adjust_contrast(self._image_tensor, factor * 1.0), device=self.device)

def adjust_color_balance(self, factor: float) -> Image:
"""
Return a new `Image` with adjusted color balance.

The original image is not modified.

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.
Marsmaennchen221 marked this conversation as resolved.
Show resolved Hide resolved

Returns
-------
image: Image
The new, adjusted image.
"""
if factor < 0:
raise OutOfBoundsError(factor, name="factor", lower_bound=ClosedBound(0))
elif factor == 1:
warnings.warn(
"Color adjustment factor is 1.0, this will not make changes to the image.",
UserWarning,
stacklevel=2,
)
return Image(
self.convert_to_grayscale()._image_tensor * (1.0 - factor * 1.0) + self._image_tensor * (factor * 1.0),
device=self.device,
)

def blur(self, radius: int) -> Image:
"""
Return a blurred version of the image.
Expand Down Expand Up @@ -498,7 +526,7 @@ def sharpen(self, factor: float) -> Image:

def invert_colors(self) -> Image:
"""
Return a new image with colors inverted.
Return a new `Image` with colors inverted.

The original image is not modified.

Expand Down
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 47 additions & 0 deletions tests/safeds/data/image/containers/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,53 @@ def test_should_raise_negative_contrast(self, resource_path: str, device: Device
Image.from_file(resolve_resource_path(resource_path), device).adjust_contrast(-1.0)


@pytest.mark.parametrize("device", _test_devices(), ids=_test_devices_ids())
class TestAdjustColor:
@pytest.mark.parametrize("factor", [2, 0.5, 0], ids=["add color", "remove color", "gray"])
@pytest.mark.parametrize(
"resource_path",
_test_images_all(),
ids=_test_images_all_ids(),
)
def test_should_adjust_colors(
self,
factor: float,
resource_path: str,
snapshot_png: SnapshotAssertion,
device: Device,
) -> None:
_skip_if_device_not_available(device)
image = Image.from_file(resolve_resource_path(resource_path), device)
image_adjusted_color_balance = image.adjust_color_balance(factor)
assert image != image_adjusted_color_balance
assert image_adjusted_color_balance == snapshot_png

@pytest.mark.parametrize(
"resource_path",
_test_images_all(),
ids=_test_images_all_ids(),
)
def test_should_not_adjust_colors(self, resource_path: str, device: Device) -> None:
_skip_if_device_not_available(device)
with pytest.warns(
UserWarning,
match="Color adjustment factor is 1.0, this will not make changes to the image.",
):
image = Image.from_file(resolve_resource_path(resource_path), device)
image_adjusted_color_balance = image.adjust_color_balance(1)
assert image == image_adjusted_color_balance

@pytest.mark.parametrize(
"resource_path",
_test_images_all(),
ids=_test_images_all_ids(),
)
def test_should_raise_negative_color_adjust(self, resource_path: str, device: Device) -> None:
_skip_if_device_not_available(device)
with pytest.raises(OutOfBoundsError, match=r"factor \(=-1.0\) is not inside \[0, \u221e\)."):
Image.from_file(resolve_resource_path(resource_path), device).adjust_color_balance(-1.0)


@pytest.mark.parametrize("device", _test_devices(), ids=_test_devices_ids())
class TestBlur:
@pytest.mark.parametrize(
Expand Down