Home > Software engineering >  Is there a way to convert multiple tiff files to numpy array at once?
Is there a way to convert multiple tiff files to numpy array at once?

Time:04-27

I'm doing a convolutional neural network classification and all my training tiles (1000 of them) are in geotiff format. I need to get all of them to a numpy array, but I only found code that will do it for one tiff file at a time.

Is there a way to convert a whole folder of tiff files at once?

Thanks!

CodePudding user response:

Try using a for loop to go through your folder

CodePudding user response:

Is your goal to get them into 1000 different numpy arrays, or to 1 numpy array? If you want the latter, it might be easiest to merge them all into a larger .tiff file, then use the code you have to process it.

If you want to get them into 1000 different arrays, this reads through a directory, uses your code to make a numpy array, and sticks them in a list.

import os
arrays_from_files = []
os.chdir("your-folder")
for name in os.listdir():
     if os.path.isfile(name):
         nparr = ##use your code here
         arrays_from_files.append(nparr)

It might be a good idea to use a dictionary and map filenames to arrays to make debugging easier down the road.

import os
arrays_by_filename = {}
os.chdir("your-folder")
for name in os.listdir():
     if os.path.isfile(name):
         nparr = ##use your code here
         arrays_by_filename[name] = nparr
  • Related