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

Fix #1442 #1445

Merged
merged 6 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def preprocess_image(

:return: The processed image and metadata (C, background_color, contrast).
"""
if isinstance(image, np.ndarray):
if not isinstance(image, np.ndarray):
image = self.convert_image_to_array(image)

# Determine background color
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import PIL
from PIL import Image
import numpy as np
import pytest
from methods import get_resource_image
from unittest import mock


from presidio_image_redactor import (
ImagePreprocessor,
ContrastSegmentedImageEnhancer,
BilateralFilter,
SegmentedAdaptiveThreshold,
Expand Down Expand Up @@ -104,3 +107,31 @@ def test_contrast_segmented_image_enhancer__improve_contrast():
assert isinstance(result[1], np.float64)
assert isinstance(result[2], np.float64)
assert result[1] <= result[2]


def test_preprocess_image_with_pil_image():
# Create a mock PIL image
pil_image = Image.new("RGB", (256, 256))

with mock.patch(
"presidio_image_redactor.ImagePreprocessor.convert_image_to_array",
side_effect=Exception("Skip the rest"),
) as mocked_function:
with pytest.raises(Exception, match="Skip the rest"):
SegmentedAdaptiveThreshold().preprocess_image(pil_image)

# Assert 'convert_image_to_array' was called
mocked_function.assert_called_once()


def test_preprocess_image_with_ndarray():
# Create a mock numpy array
array_image = np.zeros((512, 512), dtype=np.uint8)

with mock.patch(
"presidio_image_redactor.ImagePreprocessor.convert_image_to_array"
) as mocked_function:
SegmentedAdaptiveThreshold().preprocess_image(array_image)

# Assert 'convert_image_to_array' was not called
mocked_function.assert_not_called()
Loading