簡體   English   中英

讀取 TIFF 文件時出現“不是 TIFF 文件”錯誤

[英]"Not a TIFF file" error when reading TIFF files

我在train_images文件夾中有一個 .tiff 文件的文件夾。 我想使用黑白色彩空間來轉換圖像。

即使我正在閱讀 tiff 文件,我的代碼也會給我“不是 TIFF 文件”錯誤。

image_ids = df.id
image_files = glob.glob(config.BASE_PATH + "/train_images/*")

# For demonstration purposes we are taking into account 10 samples only
image_ids = image_ids[:10]
image_files = image_files[:10]

def mask2rle(img):
    '''
    img: numpy array, 1 - mask, 0 - background
    Returns run length as string formated
    '''
    pixels= img.T.flatten()
    pixels = np.concatenate([[0], pixels, [0]])
    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1
    runs[1::2] -= runs[::2]
    return ' '.join(str(x) for x in runs)
 
def rle2mask(mask_rle, shape=(1600,256)):
    '''
    mask_rle: run-length as string formated (start length)
    shape: (width,height) of array to return 
    Returns numpy array, 1 - mask, 0 - background

    '''
    s = mask_rle.split()
    starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]
    starts -= 1
    ends = starts + lengths
    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)
    for lo, hi in zip(starts, ends):
        img[lo:hi] = 1
    return img.reshape(shape).T

def basic_data_augmentations(table_name, color_space, title):
    
    # Initialize W&B
    run = wandb.init(
        project='hubmap-data-augmentation', 
        config= WANDB_CONFIG
    )
    
    wandb.run.name = title

    table = wandb.Table(
        columns=[
            'Id', 
            'Image', 
            'Image_'+ title, 
            'Mask', 
            'Mask_' + title,  
            'Image_with_Mask', 
            'Image_with_Mask_' + title
        ], 
        allow_mixed_types = True
    )

    for id, img in tqdm(zip(image_ids, image_files), total = len(image_ids)):

        img = tiff.imread(img)
        mask = rle2mask(df[df["id"]==id]["rle"].iloc[-1], (img.shape[1], img.shape[0]))
        
        plt.figure(figsize=(10,10))
        plt.axis("off")
        plt.imshow(img)
        plt.imshow(mask, cmap='coolwarm', alpha=0.5)
        plt.savefig("./image.jpg")
        plt.close()
        
        img_with_mask = cv2.cvtColor(cv2.imread("./image.jpg"), cv2.COLOR_BGR2RGB)

        table.add_data(
            id, 
            wandb.Image(img), 
            wandb.Image(cv2.cvtColor(img, color_space)),
            wandb.Image(mask),
            wandb.Image(cv2.cvtColor(cv2.cvtColor(mask, cv2.COLOR_RGBA2RGB), color_space)),
            wandb.Image(img_with_mask),
            wandb.Image(cv2.cvtColor(cv2.cvtColor(img_with_mask, cv2.COLOR_RGBA2RGB), color_space))
        )

    wandb.log({table_name : table})
    
    run.finish()

basic_data_augmentations(
    table_name = "Black and White Color Space", 
    color_space = cv2.COLOR_RGB2GRAY, 
    title = "B&W"
)

追溯:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~/anaconda3/lib/python3.9/site-packages/tifffile/tifffile.py in __init__(self, arg, mode, name, offset, size, _multifile, _useframes, _master, **kwargs)
   3010             try:
-> 3011                 byteorder = {b'II': '<', b'MM': '>', b'EP': '<'}[header[:2]]
   3012             except KeyError:

KeyError: b''

During handling of the above exception, another exception occurred:

TiffFileError                             Traceback (most recent call last)
/tmp/ipykernel_30412/159249361.py in <module>
----> 1 basic_data_augmentations(
      2     table_name = "Black and White Color Space",
      3     color_space = cv2.COLOR_RGB2GRAY,
      4     title = "B&W"
      5 )

/tmp/ipykernel_30412/2766165236.py in basic_data_augmentations(table_name, color_space, title)
     24     for id, img in tqdm(zip(image_ids, image_files), total = len(image_ids)):
     25 
---> 26         img = tiff.imread(img)
     27         mask = rle2mask(df[df["id"]==id]["rle"].iloc[-1], (img.shape[1], img.shape[0]))
     28 

~/anaconda3/lib/python3.9/site-packages/tifffile/tifffile.py in imread(files, aszarr, **kwargs)
    814 
    815         if isinstance(files, (str, os.PathLike)) or hasattr(files, 'seek'):
--> 816             with TiffFile(files, **kwargs_file) as tif:
    817                 if aszarr:
    818                     return tif.aszarr(**kwargs)

~/anaconda3/lib/python3.9/site-packages/tifffile/tifffile.py in __init__(self, arg, mode, name, offset, size, _multifile, _useframes, _master, **kwargs)
   3011                 byteorder = {b'II': '<', b'MM': '>', b'EP': '<'}[header[:2]]
   3012             except KeyError:
-> 3013                 raise TiffFileError('not a TIFF file')
   3014 
   3015             version = struct.unpack(byteorder + 'H', header[2:4])[0]

TiffFileError: not a TIFF file

路徑中的文件示例:

ls ./input/hubmap-organ-segmentation/train_images/

1123.tiff   3054.tiff   5777.tiff

文件擴展名不能保證文件類型,因為它只是文件名的一部分。 可靠地檢查文件類型的唯一方法是嘗試讀取內容本身。

有一些工具可以根據其內容猜測文件類型。 在 Linux 上, file命令可能已預先安裝,因此,您可以運行此命令來獲取結果。

$ file file_example_TIFF_1MB.tiff

file_example_TIFF_1MB.tiff: TIFF image data, little-endian

從此鏈接下載的文件

file實用程序嘗試根據以下內容猜測文件內容:

  • 文件系統測試:此測試基於從 stat 系統調用返回的結果。 程序驗證文件是否為空,或者它是否是某種特殊文件。 此測試會導致打印文件類型。
  • 魔術測試:這些測試用於檢查具有特定固定格式數據的文件。
  • 語言測試:此測試搜索可以出現在文件前幾個塊中的任何位置的特定字符串。

對於圖像文件,更強大的工具是exiftool 此命令將從幾乎任何類型的圖像格式打印您需要的大部分元數據。 在此處閱讀更多相關信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM