Skip to content

Commit

Permalink
Merge pull request matplotlib#23350 from tfpf/svgastext-nofail-fix
Browse files Browse the repository at this point in the history
TST: Fixed SVG-as-text image comparison tests.
  • Loading branch information
tacaswell authored Jul 9, 2022
2 parents 756420e + 855332a commit f06c2c3
Show file tree
Hide file tree
Showing 2 changed files with 814 additions and 753 deletions.
21 changes: 15 additions & 6 deletions lib/matplotlib/testing/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,16 @@ def calculate_rms(expected_image, actual_image):
# 16-bit depth, as Pillow converts these to RGB incorrectly.


def _load_image(path):
img = Image.open(path)
# In an RGBA image, if the smallest value in the alpha channel is 255, all
# values in it must be 255, meaning that the image is opaque. If so,
# discard the alpha channel so that it may compare equal to an RGB image.
if img.mode != "RGBA" or img.getextrema()[3][0] == 255:
img = img.convert("RGB")
return np.asarray(img)


def compare_images(expected, actual, tol, in_decorator=False):
"""
Compare two "image" files checking differences within a tolerance.
Expand Down Expand Up @@ -435,9 +445,9 @@ def compare_images(expected, actual, tol, in_decorator=False):
actual = convert(actual, cache=True)
expected = convert(expected, cache=True)

# open the image files and remove the alpha channel (if it exists)
expected_image = np.asarray(Image.open(expected).convert("RGB"))
actual_image = np.asarray(Image.open(actual).convert("RGB"))
# open the image files
expected_image = _load_image(expected)
actual_image = _load_image(actual)

actual_image, expected_image = crop_to_same(
actual, actual_image, expected, expected_image)
Expand Down Expand Up @@ -486,9 +496,8 @@ def save_diff_image(expected, actual, output):
output : str
File path to save difference image to.
"""
# Drop alpha channels, similarly to compare_images.
expected_image = np.asarray(Image.open(expected).convert("RGB"))
actual_image = np.asarray(Image.open(actual).convert("RGB"))
expected_image = _load_image(expected)
actual_image = _load_image(actual)
actual_image, expected_image = crop_to_same(
actual, actual_image, expected, expected_image)
expected_image = np.array(expected_image).astype(float)
Expand Down
Loading

0 comments on commit f06c2c3

Please sign in to comment.