Skip to content

Commit

Permalink
feat: Add adjust_color_balance method in Image (#530)
Browse files Browse the repository at this point in the history
Closes #525

### Summary of Changes

Added adjust_color_balance method for the new image class. Works just
like the adjust_color_balance method from the old image with pillow.

---------

Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com>
Co-authored-by: Lars Reimann <mail@larsreimann.com>
  • Loading branch information
3 people committed Jan 20, 2024
1 parent 96d8ecc commit dba23f9
Show file tree
Hide file tree
Showing 32 changed files with 80 additions and 5 deletions.
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
Has to be bigger than or equal to 0.
If 0 <= factor < 1, make image greyer.
If factor = 1, no changes will be made.
If factor > 1, increase color balance of image.
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
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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

0 comments on commit dba23f9

Please sign in to comment.