-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1522 from camptocamp/add-image-check-function
Add functions to test images
- Loading branch information
Showing
14 changed files
with
369 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,5 +11,4 @@ | |
!Pipfile.lock | ||
!setup.py | ||
!setup.cfg | ||
!tests | ||
**/__pycache__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ __pycache__ | |
/docs/*/build/ | ||
/.mypy_cache | ||
/acceptance_tests/tests/docker-compose.override.yaml | ||
/results/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import os | ||
from typing import TYPE_CHECKING, Any | ||
|
||
import numpy as np | ||
import skimage.color | ||
import skimage.io | ||
import skimage.metrics | ||
import skimage.transform | ||
|
||
if TYPE_CHECKING: | ||
NpNdarrayInt = np.ndarray[np.uint8, Any] | ||
else: | ||
NpNdarrayInt = np.ndarray | ||
|
||
|
||
def check_image_file( | ||
result_folder: str, | ||
image_filename_to_check: str, | ||
expected_filename: str, | ||
level: float = 1.0, | ||
generate_expected_image: bool = False, | ||
) -> None: | ||
""" | ||
Test that the `<image_filename_to_check>` corresponds to the image `tests/<image_filename>.expected.png`. | ||
If the images differ too much, `<result_folder>`/<image_filename>.result.png and | ||
`<result_folder>`/<image_filename>.diff.png are created with the corresponding content. | ||
Where `<image_filename>` is the name of the `expected_filename`. | ||
`generate_expected_image` can be set to True to generate the expected image, but it should be | ||
set to False in the committed code, because it also disable the test. | ||
""" | ||
result = skimage.io.imread(image_filename_to_check) | ||
assert result is not None, "Wrong image: " + image_filename_to_check | ||
check_image(result_folder, result, expected_filename, level, generate_expected_image) | ||
|
||
|
||
def check_image( | ||
result_folder: str, | ||
image_to_check: NpNdarrayInt, | ||
expected_filename: str, | ||
level: float = 1.0, | ||
generate_expected_image: bool = False, | ||
) -> None: | ||
""" | ||
Test that the `<image_to_check>` corresponds to the image `tests/<image_filename>.expected.png`. | ||
If they don't corresponds the images `<result_folder>`/<image_filename>.result.png and | ||
`<result_folder>`/<image_filename>.diff.png are created with the corresponding content. | ||
Where is the name if the `expected_filename`. | ||
`generate_expected_image` can be set to True to generate the expected image, but it should be | ||
set to False in the committed code, because it also disable the test. | ||
""" | ||
assert image_to_check is not None, "Image required" | ||
image_file_basename = os.path.splitext(os.path.basename(expected_filename))[0] | ||
if image_file_basename.endswith(".expected"): | ||
image_file_basename = os.path.splitext(image_file_basename)[0] | ||
result_filename = os.path.join(result_folder, f"{image_file_basename}.result.png") | ||
diff_filename = os.path.join(result_folder, f"{image_file_basename}.diff.png") | ||
|
||
if not os.path.exists(result_folder): | ||
os.makedirs(result_folder) | ||
if generate_expected_image: | ||
skimage.io.imsave(expected_filename, image_to_check) | ||
return | ||
if not os.path.isfile(expected_filename): | ||
skimage.io.imsave(result_filename, image_to_check) | ||
skimage.io.imsave(expected_filename, image_to_check) | ||
assert False, "Expected image not found: " + expected_filename | ||
expected = skimage.io.imread(expected_filename) | ||
assert expected is not None, "Wrong image: " + expected_filename | ||
|
||
score, diff = skimage.metrics.structural_similarity( | ||
expected, image_to_check, multichannel=True, full=True | ||
) | ||
diff = (255 - diff * 255).astype("uint8") | ||
|
||
if diff is None: | ||
skimage.io.imsave(result_filename, image_to_check) | ||
assert diff is not None, "No diff generated" | ||
if score < level: | ||
skimage.io.imsave(result_filename, image_to_check) | ||
skimage.io.imsave(diff_filename, diff) | ||
assert ( | ||
score >= level | ||
), f"{result_filename} != {expected_filename} => {diff_filename} ({score} > {level})" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import os | ||
|
||
import pytest | ||
|
||
from c2cwsgiutils.acceptance.image import check_image_file | ||
|
||
|
||
def test_good(): | ||
image = os.path.join(os.path.dirname(__file__), "test.expected.png") | ||
check_image_file("/results", image, os.path.join(os.path.dirname(__file__), "test.expected.png")) | ||
|
||
|
||
def test_wrong(): | ||
image = os.path.join(os.path.dirname(__file__), os.path.join(os.path.dirname(__file__), "test.wrong.png")) | ||
with pytest.raises(AssertionError): | ||
check_image_file("/results", image, os.path.join(os.path.dirname(__file__), "test.expected.png")) | ||
check_image_file( | ||
"/results", | ||
"/results/test.diff.png", | ||
os.path.join(os.path.dirname(__file__), "test.diff.expected.png"), | ||
) |