How to save a range of images when under a loop in python

81 Views Asked by At

In case there is a range of image say the dimensions is said to be (45,128,128) and I have displayed it. Seeing the result now I have decided that I need a range of images between, say, 25-36 so I end up writing a loop in order to display the interested range of image

import itertools as it
for i in it.chain(range(25,32)):
  img_disp = (x1[i]) #x1 is the input image with dimension (45,128,128)
  plt.imshow(img_disp)
  plt.show()
#displays the desired images of given range 25-32

Now this displays an array of images from 25-32, now how do i save this particluar array of images as a single (with dimension (8,128,128)) .npy file?

1

There are 1 best solutions below

0
Serge de Gosson de Varennes On

You should try:

import numpy as np

# Create an empty array to hold the images
img_array = np.empty((8, 128, 128))

# Fill the array with the desired images
for i, j in enumerate(range(25, 33)):
    img_array[i] = x1[j]

# Save the array as a .npy file
np.save('img_array.npy', img_array)