Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 41b671b

Browse files
committed
Fixed SVG-as-text image comparison tests.
SVG-as-text tests make make the figure background transparent, but its colour (i.e. under the transparent mask) is actually black. Before the expected and actual images are compared, they are converted from RGBA to RGB, which makes them both completely black (since the text is also black). As a result, the test passes even if the expected and actual images differ. Not ignoring the transparency layer fixes this problem: discard the transparency layer (if any) only if the image is completely opaque.
1 parent 40dea5a commit 41b671b

File tree

2 files changed

+811
-753
lines changed

2 files changed

+811
-753
lines changed

lib/matplotlib/testing/compare.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,13 @@ def calculate_rms(expected_image, actual_image):
371371
# 16-bit depth, as Pillow converts these to RGB incorrectly.
372372

373373

374+
def _load_image(path):
375+
img = Image.open(path)
376+
if img.mode != "RGBA" or img.getextrema()[3][0] == 255:
377+
img = img.convert("RGB")
378+
return np.asarray(img)
379+
380+
374381
def compare_images(expected, actual, tol, in_decorator=False):
375382
"""
376383
Compare two "image" files checking differences within a tolerance.
@@ -435,9 +442,9 @@ def compare_images(expected, actual, tol, in_decorator=False):
435442
actual = convert(actual, cache=True)
436443
expected = convert(expected, cache=True)
437444

438-
# open the image files and remove the alpha channel (if it exists)
439-
expected_image = np.asarray(Image.open(expected).convert("RGB"))
440-
actual_image = np.asarray(Image.open(actual).convert("RGB"))
445+
# open the image files
446+
expected_image = _load_image(expected)
447+
actual_image = _load_image(actual)
441448

442449
actual_image, expected_image = crop_to_same(
443450
actual, actual_image, expected, expected_image)
@@ -486,9 +493,8 @@ def save_diff_image(expected, actual, output):
486493
output : str
487494
File path to save difference image to.
488495
"""
489-
# Drop alpha channels, similarly to compare_images.
490-
expected_image = np.asarray(Image.open(expected).convert("RGB"))
491-
actual_image = np.asarray(Image.open(actual).convert("RGB"))
496+
expected_image = _load_image(expected)
497+
actual_image = _load_image(actual)
492498
actual_image, expected_image = crop_to_same(
493499
actual, actual_image, expected, expected_image)
494500
expected_image = np.array(expected_image).astype(float)

0 commit comments

Comments
 (0)