Skip to content

Simplify/fix save_diff_image. #23659

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions lib/matplotlib/testing/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")