Exemple #1
0
def _handle_failed_image(idiff, src_img, pngr, img_fname):
    """Writes all the necessary images when an image comparison
    failed."""
    f_base, f_ext = os.path.splitext(img_fname)

    # write out the difference file in full.
    pngw = tvtk.PNGWriter(file_name=f_base + ".diff.png")
    configure_input(pngw, idiff)
    pngw.write()

    # write the difference image scaled and gamma adjusted for the
    # dashboard.
    sz = pngr.output.dimensions
    if sz[1] <= 250.0:
        mag = 1.0
    else:
        mag = 250.0/sz[1]

    shrink = tvtk.ImageResample(interpolate=1)
    configure_input(shrink, idiff.output)
    shrink.set_axis_magnification_factor(0, mag)
    shrink.set_axis_magnification_factor(1, mag)

    gamma = tvtk.ImageShiftScale(shift=0, scale=10)
    configure_input(gamma, shrink)

    jpegw = tvtk.JPEGWriter(file_name=f_base + ".diff.small.jpg",
                            quality=85)
    configure_input(jpegw, gamma)
    jpegw.write()

    # write out the image that was generated.
    pngw.trait_set(file_name=f_base + ".test.png")
    configure_input(pngw, src_img)
    pngw.write()

    # write out a smaller version of the image that was generated.
    configure_input(shrink, idiff.input)
    jpegw.trait_set(file_name=f_base + ".test.small.jpg")
    configure_input(jpegw, shrink)
    jpegw.write()

    # write out the valid image that matched.
    configure_input(shrink, idiff.image)
    jpegw.trait_set(file_name=f_base + ".small.jpg")
    configure_input(jpegw, shrink)
    jpegw.write()
Exemple #2
0
def compare_image_with_saved_image(src_img,
                                   img_fname,
                                   threshold=10,
                                   allow_resize=True):
    """Compares a source image (src_img, which is a tvtk.ImageData)
    with the saved image file whose name is given in the second
    argument.  If the image file does not exist the image is generated
    and stored.  If not the source image is compared to that of the
    figure.  This function also handles multiple images and finds the
    best matching image.  If `allow_resize` is True then the images
    are rescaled if they are not of the same size.
    """
    f_base, f_ext = os.path.splitext(os.path.abspath(img_fname))

    if not os.path.isfile(img_fname):
        # generate the image
        pngw = tvtk.PNGWriter(file_name=img_fname, input=src_img)
        pngw.write()
        if VERBOSE:
            print "Creating baseline image '%s'." % img_fname
        return

    pngr = tvtk.PNGReader(file_name=img_fname)
    pngr.update()

    if allow_resize:
        src_resample = tvtk.ImageResample(input=src_img,
                                          interpolate=1,
                                          interpolation_mode='cubic')
        img_resample = tvtk.ImageResample(input=pngr.output,
                                          interpolate=1,
                                          interpolation_mode='cubic')
        _set_scale(src_resample, img_resample)
        idiff = tvtk.ImageDifference(input=src_resample.output,
                                     image=img_resample.output)
    else:
        idiff = tvtk.ImageDifference(input=src_img, image=pngr.output)
    idiff.update()

    min_err = idiff.thresholded_error
    img_err = min_err
    best_img = img_fname

    err_index = 0
    count = 0
    if min_err > threshold:
        count = 1
        test_failed = 1
        err_index = -1
        while 1:  # keep trying images till we get the best match.
            new_fname = f_base + "_%d.png" % count
            if not os.path.exists(new_fname):
                # no other image exists.
                break
            # since file exists check if it matches.
            pngr.file_name = new_fname
            pngr.update()
            if allow_resize:
                _set_scale(src_resample, img_resample)
            idiff.update()
            alt_err = idiff.thresholded_error
            if alt_err < threshold:
                # matched,
                err_index = count
                test_failed = 0
                min_err = alt_err
                img_err = alt_err
                best_img = new_fname
                break
            else:
                if alt_err < min_err:
                    # image is a better match.
                    err_index = count
                    min_err = alt_err
                    img_err = alt_err
                    best_img = new_fname

            count = count + 1
        # closes while loop.

        if test_failed:
            _handle_failed_image(idiff, src_img, pngr, best_img)
            _print_image_error(img_err, err_index, f_base)
            msg = "Failed image test: %f\n" % idiff.thresholded_error
            raise AssertionError, msg
    # output the image error even if a test passed
    _print_image_success(img_err, err_index)