如何修复用于保存多张图像的代码?

时间:2019-06-05 16:13:46

标签: python python-3.x

我正在通过Python 3保存多个图像的编码问题。我需要一次完成所有21张图像的保存。

我不知道如何写下正确的代码。

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 

我只能使用file.save(os.path.join(image_dir,“ image#.jpg”))将最后一个image21保存到目标文件夹中

1 个答案:

答案 0 :(得分:1)

假设image_new包含全部21张图像,并且可以按顺序对其进行区分:

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)))

使用enumerate()给出一个1到21之间的数字,然后使用format()用该数字保存图像。