I want to import 50 monochromatic .tiff images from a file using matplotlib.image
and store each in a separate 2D numpy array. Doing this for a single image is simple, and works on my system:
import matplotlib.image as mpimg
arr = mpimg.imread("file.tiff")
However, I want to do this for all 50 images (and hence get 50 arrays), and I am having trouble figuring out a simple way of doing so. I expect I can use a for loop, and I could iterate for the filename:
# imagine our files are file0.tiff, file1.tiff,..., file49.tiff
for i in range(50):
arr = mpimg.imread("file" + str(i) + ".tiff")
However, this simply (re)defines arr
50 times. The issue is that I don't know how to (automatically) create arrays named arr0, arr1, arr2...
and so on. I cannot imagine how I could do it,since all ideas I can think of to create array names involve using strings and "arr"
is not the same as arr
. That'd get me "arr0" = mpimg.imread("file0.tiff")
, which would not work.
Is there a way to insert a variable (positive/zero integer) into the left side of a line that defines a float/string/integer/bool etc.? Is there any way, actually, of inserting data into x
in x = 12
or x = numpy.array([...])
besides directly typing x
myself into the code?
I tried to use a comma, but Python apparently interprets x,y = ...
as the definition of a 2-tuple.
Could I do something like create a list ["arr0","arr1","arr2",...]
and somehow associate an image with the content of every string in the list?
I am trying to evade having to work with a single big array containing 50 images, since that entails having an array with roughly 43 million elements and the computer on which I am running this code is not particularly powerful. I still considered appending everything into such an array and then defining new arrays elementswise, but that runs into the same problem of naming the individual arrays.