0

I have a dataset of images. I have successfully iterated through my directories and subdirectories to store the images into numpy array.

I have used the following statement:

Image_array = np.array(Image_array) 

My array size is: 100x224x224

This works fine and the images get stored properly. However, I am now trying to save this numpy array into a CSV file. I have flattened the numpy array and have saved it in an array.csv file as shown below:

array = array.flatten('F') np.savetxt('array.csv', array, delimiter=',', fmt='%d') 

The above code just creates 1 CSV file, with one column with the pixel values.

I then attempted to read the CSV data back into a numpy array but the data is heavily messed up when loading as the image is just blurred. The array also displays with '.' after each number which it was not doing prior.

filename = "array.csv" data = np.loadtxt(filename, delimiter=',') new= np.array((data).reshape(100,224,224),order='F') 

Am I missing something? please assist?

1
  • 1
    np.save/load will easily save and reload the array, but the file isn't text. To use csv you have to make your array 2d, and apply a reshape after loading. arr.reshape(100,224*224) is a possible reshape. Or if you want more rows than columns (100*224, 224). Commented Nov 4, 2019 at 18:52

2 Answers 2

1

I then attempted to read the csv data back into a numpy array but the data is heavily messed up

Don't use order parameter or use it consistently. You first flatten it with F but you didn't specify order parameter when reshaping it back, which defaults to C. I think you tried to do it but order parameter placed in wrong function, it should be inside reshape.

The array also displays with '.' after each number which it was not doing prior.

Read the data with the same dtype as your prior matrix like data = np.loadtxt(filename, delimiter=',', dype=int). Since you didn't give a dtype it is converted to float I think.

Sign up to request clarification or add additional context in comments.

Comments

0

Np.flatten() creates a 1D array (which gives one csv instead of 100). Try splitting the images first and iterating through them, like this:

import numpy as np x = np.random.rand(100, 244, 244) images = [x[i,:,:] for i in range(100)] 

then your images come out like images[1], images [36], etc so you can save them like this

 def makelist(): set = [] for i in range(100): set.append(f"array{i}.csv") return set files = makelist() for file in files: for image in images: np.savetxt(file, image) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.