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

Skip to content

Commit 50948d4

Browse files
committed
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.
1 parent bf9a451 commit 50948d4

File tree

1 file changed

+6
-14
lines changed

1 file changed

+6
-14
lines changed

lib/matplotlib/testing/compare.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -506,21 +506,13 @@ def save_diff_image(expected, actual, output):
506506
raise ImageComparisonFailure(
507507
"Image sizes do not match expected size: {} "
508508
"actual size {}".format(expected_image.shape, actual_image.shape))
509-
abs_diff_image = np.abs(expected_image - actual_image)
509+
abs_diff = np.abs(expected_image - actual_image)
510510

511511
# expand differences in luminance domain
512-
abs_diff_image *= 255 * 10
513-
save_image_np = np.clip(abs_diff_image, 0, 255).astype(np.uint8)
514-
height, width, depth = save_image_np.shape
512+
abs_diff *= 10
513+
abs_diff = np.clip(abs_diff, 0, 255).astype(np.uint8)
515514

516-
# The PDF renderer doesn't produce an alpha channel, but the
517-
# matplotlib PNG writer requires one, so expand the array
518-
if depth == 3:
519-
with_alpha = np.empty((height, width, 4), dtype=np.uint8)
520-
with_alpha[:, :, 0:3] = save_image_np
521-
save_image_np = with_alpha
515+
if abs_diff.shape[2] == 4: # Hard-code the alpha channel to fully solid
516+
abs_diff[:, :, 3] = 255
522517

523-
# Hard-code the alpha channel to fully solid
524-
save_image_np[:, :, 3] = 255
525-
526-
Image.fromarray(save_image_np).save(output, format="png")
518+
Image.fromarray(abs_diff).save(output, format="png")

0 commit comments

Comments
 (0)