Save a matrix as an image without changes after recovery
<p>I need to save matrix as an image, so, that after back reading, initial and recovered matrices be the same. I have the code:</p>

<pre><code>import scipy.misc


def get_matrix(N, M):
    import random
    matrix = [[ random.randint(1, 100) for j in range(M) ] for i in range(N)]
    return matrix  

def equal(matA, matB):
    if len(matA) != len(matB):
        return False
    if (len(matA[0]) != len(matB[0])):
        return False
    for i in range(len(matA)):
        for j in range(len(matA[i])):
            if matA[i][j] != matB[i][j]:
                return False
    return True

init_matrix = get_matrix(10, 10)
scipy.misc.imsave('matrix.jpg', init_matrix)
recovered_matrix = scipy.misc.imread('matrix.jpg')
assert equal(init_matrix, recovered_matrix)
</code></pre>

<p>but after reading an image from disk, matrices are not equal. How to fix that?</p>

<p>If you're willing to use matplotlib or scikit-image, these libraries have built-in functionality for what you want.  Otherwise take a look at the imageio package.</p>

python matrix scikit-image