You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Incorrect image dimensions after reading in 3D+c TIFF #226
skimage (ie tifffile) reads this correctly, but imageio doesn't — even though I thought it also used tifffile as a backend for tiffs?
In [1]: importimageioIn [2]: fromskimageimportioIn [3]: im_iio=imageio.imread('E_z2_512_1um_CONTROL.tif')
In [4]: im_iio.shapeOut[4]: (159, 320, 512)
In [5]: im_ski=io.imread('E_z2_512_1um_CONTROL.tif')
In [6]: im_ski.shapeOut[6]: (53, 320, 512, 3)
What imageio is doing is reading the file in the array order present in the file (plane, channel, row, column), and squashing pln and ch dimensions together, while skimage is behaving as expected and producing two separate dimensions for pln and ch and (maybe a bit more than expected) moving ch to the last dimension, which is the dimension order expected by most scipy packages.
The file E_z2_512_1um_CONTROL.tif is a ImageJ hyperstack. Newer tifffile.py versions by default try to read these files in a fast mode and return all the data in the file as one contiguous array when requesting the data from first TIFF page. Other TIFF pages are discarded. This allows for memory mapping the data in the file, it significantly speeds up parsing hyperstack files with thousands TIFF pages, and it correctly reads corrupted TIFF files >4 GB.
imageio.imread('E_z2_512_1um_CONTROL.tif') returns the data from the first TIFF page. Disable the fast ImageJ hyperstack mode in imageio, e.g. imageio.imread('E_z2_512_1um_CONTROL.tif', fastij=False) to get the expected result.
skimage.io.imread('E_z2_512_1um_CONTROL.tif') returns the data of the first series of images in the file. These data are shaped according to the metadata in the file. To read the data from the first TIFF page, use skimage.io.imread('E_z2_512_1um_CONTROL.tif', img_num=0) instead. The released version of scikit-image contains an old version of tifffile without fast ImageJ hyperstack handling.
Besides this, iterating over or indexing individual TIFF pages in ImageJ hyperstacks, OME-TIFF, LSM or STK files containing high dimensional data are most often not what one wants.
@jni, do you have tifffile installed separately as a package pip list | grep tifffile? If not, can you please install it and try with imageio again? Either way, imageio should vendor the most recent tifffile.py.
@cgohlke thanks for the description! fastij doesn't quite solve things, though:
In [2]: importimageioasiioIn [3]: im=iio.imread('E_z2_512_1um_CONTROL.tif', fastij=False)
In [4]: im.shapeOut[4]: (320, 512)
In [5]: imf=iio.imread('E_z2_512_1um_CONTROL.tif', fastij=True)
In [6]: imf.shapeOut[6]: (159, 320, 512)
Indeed, the images return in array order (which is z, ch, y, x), but why are the z and ch dimensions fused together? If it returned (53, 3, 320, 512), I would certainly not have raised an issue.
@blink1073 no, it appears I am using the vendored versions both for skimage and imageio. But pip installing tifffile made no difference to the outcome above.
@jni, that is bizarre considering skimage master and the latest tifffile.py use the same file. Can you verify you have tifffile 0.11.1 and commit 2fa77fca6c1f5d7ce73cddec31f07a01cfc84b1c in skimage?
@blink1073 the vendored skimage version is written as "2017.01.12" in skimage/external/tifffile/tifffile.py. How do I check which version is being used as a plugin (vendored or installed)? Installed is 0.11.1. At any rate, it seems from @cgohlke's linked PR that skimage does some magic on top of tifffile's returned array, so it's not that surprising that its output remains magical?
self._tf=_tifffile.TiffFile(self.request.get_file(), **kwargs)
...
# Get dataifindex<0orindex>=len(self._tf):
raiseIndexError(
'Index out of range while reading from tiff file')
im=self._tf[index].asarray()
I don't know whether this is the right way to deal with this. @cgohlke@almarklein do you know why imageio uses 0, and why the tiffpage ravels the first two dimensions?
This unexpected shape for the data in the first TiffPage is an side-effect of the fast read mode for ImageJ hyperstacks. Use fastij=False to get the expected data of shape (320, 512). Use the TiffFile.asarray or TiffFile.series interfaces to get data from multiple TiffPages shaped according to the ImageJ metadata in the file.
@cgohlke@almarklein so the question is, should imageio.imread use TiffFile.asarray by default, instead of TiffPage? Based on my experience the answer is yes. =P
@jni yes, imageio assumes that an image file contains 1 of more images, making index 0 the default. Formats that support just one image will typically raise an error if the index is nonzero. Using imread() on a multi-page image file will yield the first image.
We could consider defaulting to None, if that helps in this case. But we'd have to adjust the other formats accordingly.
Also worth noting that a Format can provide more methods to extend the API of the reader/writer. Though that only applies to the use of reader = imageio.get_reader(...), not to the simpler imread and mimread().
Activity
cgohlke commentedon Feb 23, 2017
Re skimage "moving ch to the last dimension" see scikit-image/scikit-image#1496 and scikit-image/scikit-image#1498
cgohlke commentedon Feb 23, 2017
The file
E_z2_512_1um_CONTROL.tifis a ImageJ hyperstack. Newer tifffile.py versions by default try to read these files in a fast mode and return all the data in the file as one contiguous array when requesting the data from first TIFF page. Other TIFF pages are discarded. This allows for memory mapping the data in the file, it significantly speeds up parsing hyperstack files with thousands TIFF pages, and it correctly reads corrupted TIFF files >4 GB.imageio.imread('E_z2_512_1um_CONTROL.tif')returns the data from the first TIFF page. Disable the fast ImageJ hyperstack mode in imageio, e.g.imageio.imread('E_z2_512_1um_CONTROL.tif', fastij=False)to get the expected result.skimage.io.imread('E_z2_512_1um_CONTROL.tif')returns the data of the first series of images in the file. These data are shaped according to the metadata in the file. To read the data from the first TIFF page, useskimage.io.imread('E_z2_512_1um_CONTROL.tif', img_num=0)instead. The released version of scikit-image contains an old version of tifffile without fast ImageJ hyperstack handling.Besides this, iterating over or indexing individual TIFF pages in ImageJ hyperstacks, OME-TIFF, LSM or STK files containing high dimensional data are most often not what one wants.
almarklein commentedon Feb 23, 2017
Do I understand correctly that imageio is working as expected? Or is there anything we can do to improve things?
cc @blink1073
blink1073 commentedon Feb 23, 2017
@jni, do you have tifffile installed separately as a package
pip list | grep tifffile? If not, can you please install it and try withimageioagain? Either way,imageioshould vendor the most recent tifffile.py.jni commentedon Feb 25, 2017
@cgohlke thanks for the description!
fastijdoesn't quite solve things, though:Indeed, the images return in array order (which is z, ch, y, x), but why are the z and ch dimensions fused together? If it returned (53, 3, 320, 512), I would certainly not have raised an issue.
@blink1073 no, it appears I am using the vendored versions both for skimage and imageio. But pip installing tifffile made no difference to the outcome above.
jni commentedon Feb 25, 2017
Incidentally, skimage master still does "the right thing".
blink1073 commentedon Feb 25, 2017
@jni, that is bizarre considering skimage master and the latest
tifffile.pyuse the same file. Can you verify you havetifffile0.11.1 and commit2fa77fca6c1f5d7ce73cddec31f07a01cfc84b1cin skimage?jni commentedon Feb 26, 2017
@blink1073 the vendored skimage version is written as "2017.01.12" in
skimage/external/tifffile/tifffile.py. How do I check which version is being used as a plugin (vendored or installed)? Installed is 0.11.1. At any rate, it seems from @cgohlke's linked PR that skimage does some magic on top of tifffile's returned array, so it's not that surprising that its output remains magical?blink1073 commentedon Feb 26, 2017
Ah, yep.
blink1073 commentedon Feb 26, 2017
It will use the site-package vs the vendored one by design, I had forgotten about the extra processing.
blink1073 commentedon Feb 26, 2017
This is the extent of what we do in skimage:
versus imageio
jni commentedon Feb 27, 2017
@blink1073 We definitely do more to it. Here's the swapaxes magic.
But, the difference appears to be in imageio using the 0 index for the TiffFile object, which returns a TiffPage object:
I don't know whether this is the right way to deal with this. @cgohlke @almarklein do you know why imageio uses 0, and why the tiffpage ravels the first two dimensions?
cgohlke commentedon Feb 27, 2017
This unexpected shape for the data in the first TiffPage is an side-effect of the fast read mode for ImageJ hyperstacks. Use
fastij=Falseto get the expected data of shape(320, 512). Use the TiffFile.asarray or TiffFile.series interfaces to get data from multiple TiffPages shaped according to the ImageJ metadata in the file.jni commentedon Mar 2, 2017
@cgohlke @almarklein so the question is, should
imageio.imreaduseTiffFile.asarrayby default, instead ofTiffPage? Based on my experience the answer is yes. =Palmarklein commentedon Mar 2, 2017
My experience with tiff files is limited, and I don't sufficiently understand this problem; leaving this decision to @blink1073 :)
blink1073 commentedon Mar 2, 2017
I'd say adopt the skimage convention.
jni commentedon Mar 6, 2017
@almarklein regarding the TiffFile plugin, do you know why a bare imread() ends up with an index of 0 instead of None?
almarklein commentedon Mar 6, 2017
@jni yes, imageio assumes that an image file contains 1 of more images, making index
0the default. Formats that support just one image will typically raise an error if the index is nonzero. Usingimread()on a multi-page image file will yield the first image.We could consider defaulting to
None, if that helps in this case. But we'd have to adjust the other formats accordingly.Also worth noting that a Format can provide more methods to extend the API of the reader/writer. Though that only applies to the use of
reader = imageio.get_reader(...), not to the simplerimreadandmimread().does ths fix #226?
almarklein commentedon May 21, 2017
Attempt at a fix: #255