0

I just stumbled upon a weird situation with skimage.io.imread.

I was trying to open a MultiPage TIFF (dimensions: 96x512x512) like this:

import argparse
from pathlib import Path
import numpy as np
from skimage import io

def numpy_array_from_file(path):
    """ method to load numpy array from tiff file"""
    im_data = io.imread(path)
    print ("image dimensions {}".format(im_data.shape))
    return im_data

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Extract page from a MultiPage TIFF")
    parser.add_argument("tiff_file", type=str, help="3D TIFF file to open)")
    args = parser.parse_args()

    tiff_file = Path(args.tiff_file).absolute()
    numpy_array_from_file(tiff_file)

And I was obtaining in the output:

image dimensions (512, 512)

After trying many different things (because I was sure that my input image had 96 pages), I discovered that the problem was to use directly Path in the numpy_array_from_file instead of using a string. By changing the last line to:

    numpy_array_from_file(str(tiff_file))

I got the expected:

image dimensions (96, 512, 512)

So, my question is ... Anyone know why I had that behaviour? I am not very experienced in python, but I would have expected to obtain an error if Path was not appropriate in that situation.

piponazo
  • 478
  • 2
  • 4
  • 10

1 Answers1

1

Indeed, pathlib.Path is relatively new, so support in scikit-image is generally patchy. What's happening is that, because the Path is not a string, the extension isn't checked, and imageio is used instead of tifffile. The behavior of imread is different for the two libraries, with imageio preferring to only return a single plane.

This is a known issue in our issue tracker. Until it's fixed, you can use str() or explicitly call plugin='tifffile'.

Juan
  • 5,433
  • 21
  • 23
  • 1
    Thanks for the answer! At some point I thought my python skills were getting rusty, but it was not my fault after all :) – piponazo Oct 06 '19 at 19:11