Compare two "image" files checking differences within a tolerance. The two given filenames may point to files which are convertible to PNG via the `!converter` dictionary. The underlying RMS is calculated in a similar way to the `.calculate_rms` function. Parameters ------
(expected, actual, tol, in_decorator=False)
| 407 | |
| 408 | |
| 409 | def compare_images(expected, actual, tol, in_decorator=False): |
| 410 | """ |
| 411 | Compare two "image" files checking differences within a tolerance. |
| 412 | |
| 413 | The two given filenames may point to files which are convertible to |
| 414 | PNG via the `!converter` dictionary. The underlying RMS is calculated |
| 415 | in a similar way to the `.calculate_rms` function. |
| 416 | |
| 417 | Parameters |
| 418 | ---------- |
| 419 | expected : str |
| 420 | The filename of the expected image. |
| 421 | actual : str |
| 422 | The filename of the actual image. |
| 423 | tol : float |
| 424 | The tolerance (a color value difference, where 255 is the |
| 425 | maximal difference). The test fails if the average pixel |
| 426 | difference is greater than this value. |
| 427 | in_decorator : bool |
| 428 | Determines the output format. If called from image_comparison |
| 429 | decorator, this should be True. (default=False) |
| 430 | |
| 431 | Returns |
| 432 | ------- |
| 433 | None or dict or str |
| 434 | Return *None* if the images are equal within the given tolerance. |
| 435 | |
| 436 | If the images differ, the return value depends on *in_decorator*. |
| 437 | If *in_decorator* is true, a dict with the following entries is |
| 438 | returned: |
| 439 | |
| 440 | - *rms*: The RMS of the image difference. |
| 441 | - *expected*: The filename of the expected image. |
| 442 | - *actual*: The filename of the actual image. |
| 443 | - *diff_image*: The filename of the difference image. |
| 444 | - *tol*: The comparison tolerance. |
| 445 | |
| 446 | Otherwise, a human-readable multi-line string representation of this |
| 447 | information is returned. |
| 448 | |
| 449 | Examples |
| 450 | -------- |
| 451 | :: |
| 452 | |
| 453 | img1 = "./baseline/plot.png" |
| 454 | img2 = "./output/plot.png" |
| 455 | compare_images(img1, img2, 0.001) |
| 456 | |
| 457 | """ |
| 458 | actual = os.fspath(actual) |
| 459 | if not os.path.exists(actual): |
| 460 | raise Exception(f"Output image {actual} does not exist.") |
| 461 | if os.stat(actual).st_size == 0: |
| 462 | raise Exception(f"Output image file {actual} is empty.") |
| 463 | |
| 464 | # Convert the image to png |
| 465 | expected = os.fspath(expected) |
| 466 | if not os.path.exists(expected): |
searching dependent graphs…