On the other hand, your condition equals to …
while (k != 3):
    j = 0
    i = 0
    while i != 20 and j != 20 and (x[:5, :5] != mat_diff["img1taille500"][i:i+5, j:j+5]).all():
        j += 1 
    if (x[:5, :5] == mat_diff["img1taille500"][i:i+5, j:j+5]).all():
        x1 = i + 2
        y1 = j + 2 
        ...