0

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.

ygtozc
  • 3
  • 3
  • "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." - but what you're asking for won't help with that at all. It'll still take just as much time and memory to work with, but it'll be a lot more awkward. – user2357112 Nov 16 '20 at 12:33
  • Do you mean that using, say, ```big_array[i]``` (where ```big_array``` has all the data from all 50 images) to contain the data of a single image would work just as well, computationally? – ygtozc Nov 16 '20 at 12:35
  • Also, 43 million elements is really not that much by modern standards. Even on a weak and outdated machine, you can probably work with that just fine. – user2357112 Nov 16 '20 at 12:36
  • "Do you mean that using, say, big_array[i] to contain the data of a single image would work just as well, computationally?" - yeah, pretty much. I don't see how you expected using 50 separate variables to help. – user2357112 Nov 16 '20 at 12:37
  • I see, I appreciate that information. Thank you. However, I'm still curious about the general question I had- if you're using ```=``` in a line to define a variable, can you insert anything into the left-hand side to name the variable (which is on the right) automatically, instead of inputting a name (on the left) by hand? – ygtozc Nov 16 '20 at 12:39
  • "the variable (which is on the right)" - that's not how variables work. The variable is the thing on the left. The thing on the right is an object you're binding to the variable. There are limited circumstances where it is possible to dynamically name variables, but in almost every possible circumstance, it's better to just use an actual data structure instead of dynamic variable names. – user2357112 Nov 16 '20 at 12:46
  • If you are determined to name each image variable arrN, this answer will help: https://stackoverflow.com/a/53503041/3657941 –  Nov 16 '20 at 12:46
  • @user2357112supportsMonica And for reference I thought it might be detrimental to performance if I needed to call a 43-M element array multiple times in my code. I do not know how memory management in code works at all, my guess that calling `array[i]` calls the entire array first was incorrect, I presume. – ygtozc Nov 16 '20 at 12:49
  • @DavidCullen Thank you! Thinking 'aloud', I suppose changing `'a'` in that example to the corresponding iterated filename I need would give me what I wanted here. Much appreciated! I'm new to Stack Overflow and I don't believe I can give reputation for comments yet. If you link to that post as an answer, I will mark it as resolved. – ygtozc Nov 16 '20 at 12:53
  • Just referring to `array[i]` doesn't cost time or memory proportional to the size of `array`. It's not even expensive proportional to the size of `array[i]`. It's constant time. Actually *doing things* with `array[i]` will take more time depending on what you're doing, but the cost won't be proportional to the size of `array`. – user2357112 Nov 16 '20 at 12:55
  • @user2357112supportsMonica I really appreciate your patience with me, thank you for the information and correction. – ygtozc Nov 16 '20 at 12:55

0 Answers0