如何使用for循环保存图像?

时间:2019-05-02 15:44:42

标签: python image for-loop rename tiff

我有一个细分问题的数据集。数据以.h5文件扩展名存储。每个数据点,即“ 1234.h5”包括图像和遮罩以及有关该图像的其他信息。我想做的是提取图像及其蒙版并命名 图片_0,图片_1,... mask_0,mask_1,... 试图编写一个for循环来做到这一点,但失败了。

我设法一张一张地提取图像和遮罩。但不在for循环中。

imagenames = pd.read_hdf("imagenames.h5")
image_paths = list(imagenames["annotated_image_path"])

# Read in the image and the mask
def get_visible_and_mask(image_path):
    with h5py.File(image_path, 'r') as annotated_img:
        img_visible = annotated_img["georef_img"]["layers"]["visible"]["array"][()]
        mask = annotated_img["georef_img"]["layers"]["mask"]["array"][()]
    return img_visible, mask

# Get a random image_path
image_path = random.choice(image_paths)
img_visible, mask = get_visible_and_mask(image_path)

#Save the random image and the mask
imageio.imwrite('img_vis.tif', img_visible)
imageio.imwrite('mask.tif', mask)

0 个答案:

没有答案