From e08f44b273f4a50ab28f328a058ca7682e32ecf3 Mon Sep 17 00:00:00 2001 From: Omri Mendels Date: Sun, 1 Sep 2024 18:05:16 +0300 Subject: [PATCH] Fix #1442 (#1445) --- .../image_processing_engine.py | 2 +- .../test_image_processing_engine.py | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/presidio-image-redactor/presidio_image_redactor/image_processing_engine.py b/presidio-image-redactor/presidio_image_redactor/image_processing_engine.py index 1a85ce21f..f687a75bb 100644 --- a/presidio-image-redactor/presidio_image_redactor/image_processing_engine.py +++ b/presidio-image-redactor/presidio_image_redactor/image_processing_engine.py @@ -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 diff --git a/presidio-image-redactor/tests/integration/test_image_processing_engine.py b/presidio-image-redactor/tests/integration/test_image_processing_engine.py index 616bdc6b6..8d281016a 100644 --- a/presidio-image-redactor/tests/integration/test_image_processing_engine.py +++ b/presidio-image-redactor/tests/integration/test_image_processing_engine.py @@ -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, @@ -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()