Find the best alignment of two images that minimizes the differences.

    Returns (diff, alignments) where ``diff`` is a difference map, and
    ``alignments`` is a tuple ((x1, y2), (x2, y2)).

    See ``diff()`` for the description of the alignment numbers.

def best_diff(img1, img2, opts):
    """Find the best alignment of two images that minimizes the differences.

    Returns (diff, alignments) where ``diff`` is a difference map, and
    ``alignments`` is a tuple ((x1, y2), (x2, y2)).

    See ``diff()`` for the description of the alignment numbers.
    """
    w1, h1 = img1.size
    w2, h2 = img2.size
    w, h = min(w1, w2), min(h1, h2)
    best = None
    best_value = 255 * w * h + 1

    xr = abs(w1 - w2) + 1
    yr = abs(h1 - h2) + 1

    p = Progress(xr * yr, timeout=opts.timeout)
    for x in range(xr):
        if w1 > w2:
            x1, x2 = x, 0
        else:
            x1, x2 = 0, x
        for y in range(yr):
            if h1 > h2:
                y1, y2 = y, 0
            else:
                y1, y2 = 0, y
            p.next()
            this = diff(img1, img2, (x1, y1), (x2, y2))
            this_value = diff_badness(this)
            if this_value < best_value:
                best = this
                best_value = this_value
                best_pos = (x1, y1), (x2, y2)
    return best, best_pos