pythonimage-processingnumpy-ndarraysave-imageloadimage

Saving an image as a numpy array in a file and loading it back from the file as an image using Python


I am trying to convert an image to a numpy array and save it as a text/csv file. I am then trying to load the contents of the text/csv file back into an image.

In the whole process, the dimension, datatype pixel values must not change in order to reconstruct the original image accurately (without distortions).

What I have so far-

testim = cv2.imread('img.jpg') #reading the input image

numpyimg = np.array(testim) # Saving as a numpy array

# Checking the shape and image
cv2_imshow(numpyimg)
print(numpyimg.shape)

# Trying to save in csv
for i in numpyimg:
  np.savetxt(fname="image_array.csv", delimiter=",", X=i)

# Check generated csv file after loading it

image_array = np.loadtxt(
    fname="image_array.csv", delimiter=","
)

print("NumPy array: \n", image_array)
print("Shape: ", image_array.shape)
print("Data Type: ", image_array.dtype.name)

When I print the contents of the saved file what I see -

NumPy array that I could saved in a file: 
 [[ 70. 176. 153.]
 [ 63. 170. 144.]
 [ 57. 167. 139.]
 ...
 [ 69. 118.  80.]
 [ 67. 117.  77.]
 [ 64. 114.  74.]]
Shape:  (1040, 3)

The array of the original image though-

array([[[ 78, 120, 165],
        [ 63, 105, 150],
        [ 48,  91, 134],
        ...,
        [ 22,  80,  51],
        [ 35,  91,  62],
        [ 49, 105,  76]],

       [[ 77, 122, 160],
        [ 62, 109, 147],
        [ 50,  95, 132],
        ...,
        [ 24,  84,  54],
        [ 29,  87,  58],
        [ 38,  96,  67]],

       [[ 73, 124, 150],
        [ 66, 120, 143],
        [ 63, 116, 137],
        ...,
        [ 28,  90,  60],
        [ 26,  86,  56],
        [ 27,  87,  57]],

       ...,

        [ 69, 118,  80],
        [ 67, 117,  77],
        [ 64, 114,  74]]], dtype=uint8)
shape: (780, 1040, 3)

These don't look to be the same and I fail to understand what is going wrong.

Is there an easier and more accurate way to go about this?

I have been stuck on this for a long time. Any help is appreciated!


Solution

  • I was able to figure the solution out after a lot of trial and error. This is what helped me solve my problem-

    from PIL import Image
    
    # Create an empty text file before this step
    with open('image_array.txt', 'w') as outfile:
        for slice_2d in numpyimg:
            np.savetxt(outfile, slice_2d)
    
    new_data = np.loadtxt('image_array.txt')
    
    new_data=new_data.reshape((780,1040,3))
    
    img = Image.fromarray(new_data.astype(np.uint8),'RGB')
    img.save('try.jpg')
    img.show()