def best_diff(img1, img2, opts):
w1, h1 = img1.size<EOL>w2, h2 = img2.size<EOL>w, h = min(w1, w2), min(h1, h2)<EOL>best = None<EOL>best_value = <NUM_LIT:255> * w * h + <NUM_LIT:1><EOL>xr = abs(w1 - w2) + <NUM_LIT:1><EOL>yr = abs(h1 - h2) + <NUM_LIT:1><EOL>p = Progress(xr * yr, timeout=opts.timeout)<EOL>for x in range(xr):<EOL><INDENT>if w1 > w2:<EOL><INDENT>x1, x2 = x, <NUM_LIT:0><EOL><DEDENT>else:<EOL><INDENT>x1, x2 = <NUM_LIT:0>, x<EOL><DEDENT>for y in range(yr):<EOL><INDENT>if h1 > h2:<EOL><INDENT>y1, y2 = y, <NUM_LIT:0><EOL><DEDENT>else:<EOL><INDENT>y1, y2 = <NUM_LIT:0>, y<EOL><DEDENT>p.next()<EOL>this = diff(img1, img2, (x1, y1), (x2, y2))<EOL>this_value = diff_badness(this)<EOL>if this_value < best_value:<EOL><INDENT>best = this<EOL>best_value = this_value<EOL>best_pos = (x1, y1), (x2, y2)<EOL><DEDENT><DEDENT><DEDENT>return best, best_pos<EOL>
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.