处理后删除原始图像

时间:2017-12-23 20:43:54

标签: python-3.x image-processing python-imaging-library delete-file connected-components

我的连接组件功能采用原始图像处理它并生成处理过的图像如何从文件夹中删除原始图像并保留仅处理过的图像。

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 = 'D:/Python36/Fold/'
        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 = "D:/Python36/Fold/"
    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()

1 个答案:

答案 0 :(得分:1)

如果我正确理解了您的问题,您可以在完成处理后立即使用os.remove()删除文件。

然后你的主要功能就是:

def main():
    path = "D:/Python36/Fold/"
    for root, dirs, files in os.walk(path):
        for file_ in files:
            full_file_path = os.path.join(root, file_)
            img = Image.open(full_file_path)
            img = img.point(lambda p: p > 190 and 255)
            img = img.convert('1')
            (labels, output_img) = run(root, img)
            os.remove(full_file_path)