Daniel L
Daniel L

Reputation: 31

How to fix code for saving multiple images?

I am working on an coding problem with saving multiple images by Python 3. I need to get all my 21 images save finished at once.

I don’t know how to write down the correct code.

image_new = ... 
img_dir = 'C:\Users\...' 
for i in image_new:
     j = np.array(i) 
     … 
     j = Image.fromarray(j.astype(np.uint8)) 
     j.save(os.path.join(img_dir, "image1-21.jpg")) #this line has to be fixed 

I can only save the last image21 into the destination folder with using file.save (os.path.join(image_dir, “image#.jpg”))

Upvotes: 0

Views: 127

Answers (1)

economy
economy

Reputation: 4251

Assuming image_new contains all 21 images, and that they can be distinguished by their order:

image_new = ... 
img_dir = 'C:\Users\...' 
for num, i in enumerate(image_new):
     j = np.array(i) 
     … 
     j = Image.fromarray(j.astype(np.uint8)) 
     j.save(os.path.join(img_dir, "image-{}.jpg".format(num + 1)))

Using enumerate() to come up with a number from 1-21, and saving the image with that number using format().

Upvotes: 1

Related Questions