I have 50000 grayscale images with 56*56 pixels each one. I need to flatten images and stakck it in the same array with dimensions 50000*3136 after that export the file as .CSV. I am doing this code which load all images in the memory which can cause a problem if we have not enough memory space.
imagesToArray[pathsrc_, pathdes_] := Module[{filesList, data}, (
(* pathsrc_ : list of images path *)
(* pathdes_ : CSV file path*)
filesList = FileNames["*.png", pathsrc];
Print["Number of images :", Length@filesList];
data =
Table[Flatten[ImageData[Import[filesList[[i]]]]], {i, 1,
Length@filesList}];
Export[pathdes, data];
data
)]
Is there any alternative to do it faster without memory problem?
data = Flatten [ ImageData /@ Import /@ fileList ]
instead ofTable[ ]
? Am I missing something? $\endgroup$Export
to achieve that though. $\endgroup$Import
does, you can try using the built-in function that Import ultimately calls to read in a PNG file, which isImage`ImportExportDump`ImageReadPNG
. It returns a list containing the image imported. You can use e.g.First@Image`ImportExportDump`ImageReadPNG[pathToImage]
to get each image. It is roughly 10x faster than usingImport
. $\endgroup$