【问题标题】:Extract frames from multiple tiff format images从多个 tiff 格式图像中提取帧
【发布时间】:2020-12-05 16:24:54
【问题描述】:

我有一个需要分解的 tiff 图像数据集。每个文件有 50 帧,目前我正在一一分解,但与我拥有的图像数量相比,分解每一个都需要很长时间。我的目标是文件夹中的每个 tiff 文件,我想将它们分解并存储在一个单独的文件夹中,每个 tiff 图像将始终有 50 帧,例如:

在 C:\Dataset\tiff-images\ 内

我有 tiff-image1、tiff-image2、tiffimage3、tiffimage4。

仍然在同一个目录中,我有以下文件夹:tiff-image1、tiff-image2、tiff-image3、tiff-image4。

基本上,我想要简单地遍历目录中尽可能多的 tiff 图像,并通过创建一个文件夹将它们分解到各自的文件夹中,以防万一。

我现在正在尝试的方式并不完全是最佳的,并且需要很长时间才能完成此过程:

imagepath = "tiff-image1.tif"
path = "C:/Dataset/tiff-images/" + imagepath
img = Image.open(path)

for i in range(50):
    try:
        img.seek(i)
        img.save('C:/Dataset/tiff-images/tiff-image1-folder1/tiff-image1-decomp.tif%s.tif'%(i,))
    except EOFError:
        break

imagepath = "tiff-image2.tif"
path = "C:/Dataset/tiff-images/" + imagepath
img = Image.open(path)

for i in range(50):
    try:
        img.seek(i)
        img.save('C:/Dataset/tiff-images/tiff-image2-folder2/tiff-image2-decomp.tif%s.tif'%(i,))
    except EOFError:
        break

imagepath = "tiff-image3.tif"
path = "C:/Dataset/tiff-images/" + imagepath
img = Image.open(path)

for i in range(50):
    try:
        img.seek(i)
        img.save('C:/Dataset/tiff-images/tiff-image3-folder3/tiff-image3-decomp.tif%s.tif'%(i,))
    except EOFError:
        break

imagepath = "tiff-image4.tif"
path = "C:/Dataset/tiff-images/" + imagepath
img = Image.open(path)

for i in range(50):
    try:
        img.seek(i)
        img.save('C:/Dataset/tiff-images/tiff-image4-folder4/tiff-image4-decomp.tif%s.tif'%(i,))
    except EOFError:
        break

【问题讨论】:

    标签: python image image-processing python-imaging-library tiff


    【解决方案1】:

    您可以使用os 模块进行此类自动化。 检查这个:

    import os
    from PIL import Image
    
    # enter the main folder path here
    path = './'
    
    lsdir = os.listdir(path)
    
    for f in lsdir:
        if os.path.isfile(f):
            file, ext = os.path.splitext(f)
            if ext in ['.tif', '.tiff']:
                fldr = os.path.join(path, file)
                if not os.path.isdir(fldr):
                    os.mkdir(fldr)
    
                imgpath = os.path.join(path, f)
                img = Image.open(imgpath)
                for i in range(50):
                    try:
                        img.seek(i)
                        img.save(f'{fldr}/decomp{i}.tif')
                    except EOFError:
                        break
    

    【讨论】:

    • os 或 glob 如果您只需要过滤掉图像并进行循环搜索。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多