简体   繁体   中英

Imwrite saves only last image of the for loop

This is a code looping through all the images in a folder resizing them to a size of 100x100 and saves them in a different folder. The problem is that it saves only the last image of the loop/folder. How can I save all the images at a different location?

import cv2
import glob
import uuid

images = glob.glob(r"C:\\Users\\DELL\\Desktop\\Akshar\\ALA\\*.jpg")

for i in images:
   
    img = cv2.imread(i, 1)
  
    res = cv2.resize(img, (100, 100))
   
    cv2.imshow("Batch Image", res)
   
    cv2.waitKey(1000)
   
    cv2.destroyAllWindows()
   
    cv2.imwrite(r'C:\\Users\\DELL\\Desktop\\Akshar\\ALA1\\image.jpg', res)

One solution could be to use string format to dynamically create the image filename, either through '%s %s' % ('one', 'two') or '{} {}'.format('one', 'two')

img = cv2.imread(i, 1)
res = cv2.resize(img, (100, 100))
cv2.imshow("Batch Image", res)
cv2.waitKey(1000)
cv2.destroyAllWindows()
# Update this line to include a string format.
cv2.imwrite(r'C:\Users\DELL\Desktop\Akshar\ALA1\image_%s.jpg' % i, res)

If i is equal to 2, this will give you a filename of:

'C:\Users\DELL\Desktop\Akshar\ALA1\image_2.jpg'

You can also remove the double backslash since you're using a raw string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM