Skip to content

Commit

Permalink
test: remove unused resource
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-reimann committed Jul 7, 2023
1 parent a8bf4bb commit dbba115
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 43 deletions.
20 changes: 8 additions & 12 deletions src/safeds/data/image/containers/_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
from pathlib import Path
from typing import Any, BinaryIO

import numpy as np
import PIL
from PIL import ImageEnhance, ImageFilter, ImageOps
from PIL.Image import Image as PillowImage
from PIL.Image import open as open_image
from skimage.util import random_noise

from safeds.data.image.typing import ImageFormat

Expand Down Expand Up @@ -305,23 +307,17 @@ def adjust_brightness(self, factor: float) -> Image:
image_copy._image = ImageEnhance.Brightness(image_copy._image).enhance(factor)
return image_copy

def add_gaussian_noise(self, standard_deviation: float, opacity: float = 1.0) -> Image:
def add_gaussian_noise(self, standard_deviation: float) -> Image:
if standard_deviation < 0:
raise ValueError("Standard deviation has to be 0 or bigger.")

if opacity < 0 or opacity > 1:
raise ValueError("Opacity has to be between 0 and 1.")

if opacity == 0:
warnings.warn(
"Opacity is 0, this will not make changes to the image.",
UserWarning,
stacklevel=2,
)
# noinspection PyTypeChecker
image_as_array = np.asarray(self._image)
noisy_image_as_array = random_noise(image_as_array, mode="gaussian", var=standard_deviation**2, rng=42, clip=True)
noisy_image = PIL.Image.fromarray(np.uint8(255 * noisy_image_as_array))

image_copy = copy.deepcopy(self)
img_noise = PIL.Image.effect_noise((image_copy.width, image_copy.height), standard_deviation)
image_copy._image = PIL.Image.blend(image_copy._image.convert("RGB"), img_noise.convert("RGB"), opacity)
image_copy._image = noisy_image
return image_copy

def adjust_contrast(self, factor: float) -> Image:
Expand Down
Binary file removed tests/resources/image/blend/blended.png
Binary file not shown.
Binary file removed tests/resources/image/blend/original_scaled.png
Binary file not shown.
Binary file added tests/resources/image/noise/noise_0.7.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 removed tests/resources/image/noise/noise_0.7_1.0.png
Binary file not shown.
Binary file added tests/resources/image/noise/noise_2.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 removed tests/resources/image/noise/noise_2.5_0.6.png
Binary file not shown.
Binary file removed tests/resources/white.jpg/white.png
Binary file not shown.
42 changes: 11 additions & 31 deletions tests/safeds/data/image/containers/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,49 +329,29 @@ def test_should_invert_colors(self, image: Image, expected: Image) -> None:

class TestGaussianNoise:
@pytest.mark.parametrize(
("image", "standard_deviation", "opacity"),
("image", "standard_deviation"),
[
(Image.from_png_file(resolve_resource_path("image/boy.png")), 0.7, 1.0),
(Image.from_png_file(resolve_resource_path("image/boy.png")), 2.5, 0.6),
(Image.from_png_file(resolve_resource_path("image/boy.png")), 0.7),
(Image.from_png_file(resolve_resource_path("image/boy.png")), 2.5),
],
ids=["one", "two"],
)
def test_should_add_noise(self, image: Image, standard_deviation: float, opacity: float) -> None:
def test_should_add_noise(self, image: Image, standard_deviation: float) -> None:
expected = Image.from_png_file(
resolve_resource_path("image/noise/noise_" + str(standard_deviation) + "_" + str(opacity) + ".png"),
resolve_resource_path("image/noise/noise_" + str(standard_deviation) + ".png"),
)
image = image.add_gaussian_noise(standard_deviation, opacity)
image = image.add_gaussian_noise(standard_deviation)

assert image == expected

@pytest.mark.parametrize(
("image", "standard_deviation", "opacity"),
[(Image.from_png_file(resolve_resource_path("image/boy.png")), -1, 1.0)],
("image", "standard_deviation"),
[(Image.from_png_file(resolve_resource_path("image/boy.png")), -1)],
ids=["sigma below zero"],
)
def test_should_raise_standard_deviation(self, image: Image, standard_deviation: float, opacity: float) -> None:
def test_should_raise_standard_deviation(self, image: Image, standard_deviation: float) -> None:
with pytest.raises(ValueError, match="Standard deviation has to be 0 or bigger."):
image.add_gaussian_noise(standard_deviation, opacity)

@pytest.mark.parametrize(
("image", "standard_deviation", "opacity"),
[
(Image.from_png_file(resolve_resource_path("image/boy.png")), 2, -1),
(Image.from_png_file(resolve_resource_path("image/boy.png")), 2, 2),
],
ids=["opacity below zero", "opacity above zero"],
)
def test_should_raise_opacity(self, image: Image, standard_deviation: float, opacity: float) -> None:
with pytest.raises(ValueError, match="Opacity has to be between 0 and 1."):
image.add_gaussian_noise(standard_deviation, opacity)

@pytest.mark.parametrize(
("image", "standard_deviation", "opacity"),
[(Image.from_png_file(resolve_resource_path("image/boy.png")), 1, 0)],
ids=["opacity zero"],
)
def test_should_warn(self, image: Image, standard_deviation: float, opacity: float) -> None:
with pytest.warns(UserWarning, match="Opacity is 0, this will not make changes to the image."):
image.add_gaussian_noise(standard_deviation, opacity)
image.add_gaussian_noise(standard_deviation)


class TestBlur:
Expand Down

0 comments on commit dbba115

Please sign in to comment.