1

我有一个文件夹“Images-2”,它有超过100个子文件夹,这些子文件夹由每个文件夹一个图像组成。 def main()打开每个图像,然后def run(img)拍摄图像并对其进行处理,但现在我无法将该图像保存在其子文件夹中。迭代文件夹打开,逐个处理并保存图像

例如def main C:/Images-2/1/1.png(1是文件夹的名称,所以我有100个文件夹中的图像-2)

如果条件将保存处理后的图像(零。 png)in folder Images-2/1/

它如何工作100个文件夹,每个文件夹1个图像?

def run(img): 
    data = img.load() 
    width, height = img.size 
    output_img = Image.new("RGB", (100, 100)) 
    Zero=np.zeros(shape=(100, 100),dtype=np.uint8) 

    for (x, y) in labels: 
      component = uf.find(labels[(x, y)]) 
      labels[(x, y)] = component 
      path='C:/Python27/cclabel/Images-2/' 
      if labels[(x, y)]==0: 
       Zero[y][x]=int(255) 
       Zeroth = Image.fromarray(Zero) 
       for root, dirs in os.walk(path): 
        print root 
        print dirs 
        Zeroth.save(path+'Zero'+'.png','png') 
def main(): 
    # Open the image 
    path="C:/Python27/cclabel/Images-2/" 
    for root, dirs, files in os.walk(path): 
     for file_ in files: 
      img = Image.open(os.path.join(root, file_)) 
      img = img.point(lambda p: p > 190 and 255) 
      img = img.convert('1') 
      (labels, output_img) = run(img) 

if __name__ == "__main__": main() 
+0

在'run'中定义的变量'labels'在哪里?我认为你应该将'root'传递给'run',然后从'run'中删除'os.walk',然后执行'Zeroth.save(os.path.join(root,'Zero.png'), 'png')'将“零”图像存储到您加载图像的同一目录中。 –

+0

那是我无法保存正确的道路上它只是把最后一个文件夹的图像处理它并将它保存每个文件夹上 –

回答

0

您打电话给os.walk两次。那是你的问题。这就是我的意见在我的意见:

def run(dirname, img): 
    data = img.load() 
    width, height = img.size 
    output_img = Image.new("RGB", (100, 100)) 
    Zero=np.zeros(shape=(100, 100), dtype=np.uint8) 

    for (x, y) in labels: 
     component = uf.find(labels[(x, y)]) 
     labels[(x, y)] = component 
     path = 'C:/Python27/cclabel/Images-2/' 
     if labels[(x, y)] == 0: 
      Zero[y][x] = 255 
      Zeroth = Image.fromarray(Zero) 
      Zeroth.save(os.path.join(dirname, 'Zero.png'), 'png') 


def main(): 
    path = "C:/Python27/cclabel/Images-2/" 
    for root, dirs, files in os.walk(path): 
     for file_ in files: 
      img = Image.open(os.path.join(root, file_)) 
      img = img.point(lambda p: p > 190 and 255) 
      img = img.convert('1') 
      (labels, output_img) = run(root, img) 


if __name__ == "__main__": 
    main() 
+0

错误的问题:格式=分机[转] KeyError异常:“” –

+0

第一个就是一个烂摊子在括号中,我在我的帖子中解决了这个问题。对于第二个评论:你是什么意思?你能否详细说明一下? –

+0

由于它的工作完美 –

相关问题