def compare_binary_images(image1, image2):
    """
    Return the fraction of pixels that are the same in the two images.
    """
    if not image1.size == image2.size:
        return 0.0
    pix1 = image1.load()
    pix2 = image2.load()
    num_same = 0
    num_total = image1.size[0] * image1.size[1]
    for ix in range(image1.size[0]):
        for iy in range(image1.size[1]):
            if pix1[ix, iy] == pix2[ix, iy]:
                num_same += 1
    return float(num_same / num_total)