[英]Imwrite saves only last image of the for loop
这是一个循环遍历文件夹中所有图像的代码,将它们调整为 100x100 的大小并将它们保存在不同的文件夹中。 问题是它只保存循环/文件夹的最后一个图像。 如何将所有图像保存在不同的位置?
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)
一种解决方案是使用字符串格式动态创建图像文件名,通过'%s %s' % ('one', 'two')
或'{} {}'.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)
如果i
等于 2,这将为您提供以下文件名:
'C:\Users\DELL\Desktop\Akshar\ALA1\image_2.jpg'
您还可以删除双反斜杠,因为您使用的是原始字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.