首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >打开迭代文件夹,逐一处理和保存图像。

打开迭代文件夹,逐一处理和保存图像。
EN

Stack Overflow用户
提问于 2017-12-03 07:27:50
回答 1查看 588关注 0票数 1

我有一个文件夹“图片-2”,其中有100多个子文件夹,这些子文件夹包括一个图像每个文件夹。def main()打开每个图像,def run(img)接收图像并对其进行处理,但现在我无法将该图像保存在它的子文件夹中。

例如:/ def main c:/def main-2/1/1.png (1是文件夹名,所以我在图-2中有100个文件夹)

如果条件将保存处理过的图像(zero.png)在文件夹图像-2/1/

如何处理100个文件夹,每个文件夹一个图像?

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-03 12:30:38

你要给os.walk打两次电话。这是你的问题。这就是我在评论中的意思:

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47616240

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档