从所有图像中裁剪扇区并在循环中以不同的名称保存它们

cropping sectors from all the images and saving them with different names in the loop

我正在尝试从所有图像中裁剪扇区(图像包含圆形对象)并尝试在 loop.Here 中以不同的名称保存它们是我使用 python 3.6 和 scikit 图像的代码

for filename in os.listdir("directory1"):
    image=io.imread(os.path.join("path1",filename))
'''get coordinates for polygon & circle,create masks for circle and polygon'''
'''(r0,c0),(r1,c1),(r2,c2) are circle center , coord's of sector''' 
  for k in range(7):
    imagevar=image
    rr, cc = draw.polygon([r0, r1[K], r1[K+1], r0],
                      [c0, c1[K], c1[K+1], c0], shape=mask_poly.shape)            
    mask_poly[rr, cc] = 1
    image_mask = mask_circle & mask_poly
    mk=numpy.logical_not(image_mask)
    imagevar[mk]=0
    path2='*pathforsamples*' #different from path1
    NewfileNameToSave = *path2*+fileNameToSave + '_'+ str(K+1) + '.jpg'
    io.imsave(NewfileNameToSave,imagevar)
    mask_poly[rr, cc] = 0

文件名没有问题,但图像有问题。我从 1st 得到第一个扇区 迭代,但从第二次迭代开始,我得到一个黑色图像和一个警告,上面写着 "filename_2.jpg is a low contrast image"。我猜第一个扇区被用于剩余的迭代,即使我在循环开始时分配了 "imagevar=image" 。那么,在"imsave"或代码有误后,有什么办法可以卸载图像吗?

找到了我的问题的解决方案。在第二个 for 循环中,我们可以使用 imread

再次读取图像