From 50948d40b2e42a46b44e21d90ad45cb71c5975f8 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Thu, 18 Aug 2022 11:43:05 +0200 Subject: [PATCH] Simplify/fix save_diff_image. - The previous `*255*10` factor was necessary when using the old _png module to read images, which read them as 0-1 floats, the 255 factor was needed prior to conversion to uint8. Now we use pillow everywhere in matplotlib.testing.compare and work with uint8 throughout, so the extra 255 is incorrect (it basically caused any slight difference to be fully saturated). - We don't use _png to write the diffs anymore, and pillow handles RGB just fine, so we don't need to add an alpha channel if it's not there. --- lib/matplotlib/testing/compare.py | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/lib/matplotlib/testing/compare.py b/lib/matplotlib/testing/compare.py index 4afb3c6ecea2..c2ed8247d93b 100644 --- a/lib/matplotlib/testing/compare.py +++ b/lib/matplotlib/testing/compare.py @@ -506,21 +506,13 @@ def save_diff_image(expected, actual, output): raise ImageComparisonFailure( "Image sizes do not match expected size: {} " "actual size {}".format(expected_image.shape, actual_image.shape)) - abs_diff_image = np.abs(expected_image - actual_image) + abs_diff = np.abs(expected_image - actual_image) # expand differences in luminance domain - abs_diff_image *= 255 * 10 - save_image_np = np.clip(abs_diff_image, 0, 255).astype(np.uint8) - height, width, depth = save_image_np.shape + abs_diff *= 10 + abs_diff = np.clip(abs_diff, 0, 255).astype(np.uint8) - # The PDF renderer doesn't produce an alpha channel, but the - # matplotlib PNG writer requires one, so expand the array - if depth == 3: - with_alpha = np.empty((height, width, 4), dtype=np.uint8) - with_alpha[:, :, 0:3] = save_image_np - save_image_np = with_alpha + if abs_diff.shape[2] == 4: # Hard-code the alpha channel to fully solid + abs_diff[:, :, 3] = 255 - # Hard-code the alpha channel to fully solid - save_image_np[:, :, 3] = 255 - - Image.fromarray(save_image_np).save(output, format="png") + Image.fromarray(abs_diff).save(output, format="png")