cgohlke / czifile

Read Carl Zeiss(r) Image (CZI) files
https://pypi.org/project/czifile
Other
27 stars 8 forks source link

imread error when passing a string #6

Closed haesleinhuepf closed 10 months ago

haesleinhuepf commented 1 year ago

Hi @cgohlke ,

I'm retrieving an error when passing a string to the imread function and presume it's a bug. This example works well:

import czifile
from pathlib import Path
image = czifile.imread(Path("../../data/PupalWing.czi"))

But this throws an error:

import czifile
image = czifile.imread("../../data/PupalWing.czi")
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[12], line 2
      1 import czifile
----> 2 image = czifile.imread("../../data/PupalWing.czi")

File ~\mambaforge\envs\bio39\lib\site-packages\czifile\czifile.py:220, in imread(filename, *args, **kwargs)
    214 def imread(filename, *args, **kwargs):
    215     """Return image data from CZI file as numpy array.
    216 
    217     'args' and 'kwargs' are arguments to the CziFile.asarray function.
    218 
    219     """
--> 220     with CziFile(filename) as czi:
    221         result = czi.asarray(*args, **kwargs)
    222     return result

File ~\mambaforge\envs\bio39\lib\site-packages\czifile\czifile.py:275, in CziFile.__init__(self, arg, multifile, filesize, detectmosaic)
    272 if multifile and self.header.file_part and isinstance(arg, basestring):
    273     # open master file instead
    274     self._fh.close()
--> 275     name, _ = match_filename(arg)
    276     self._fh = FileHandle(name)
    277     self.header = Segment(self._fh, 0).data()

File ~\mambaforge\envs\bio39\lib\site-packages\czifile\czifile.py:1148, in match_filename(filename)
   1145 match = re.search(r'(.*?)(?:\((\d+)\))?\.czi$',
   1146                   filename, re.IGNORECASE).groups()
   1147 name = match[0] + '.czi'
-> 1148 part = int(match[1]) if len(match) > 1 else 0
   1149 return name, part

TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

If you want to reproduce this, you can download the example image here.

Thanks!

Best, Robert

cgohlke commented 10 months ago

Thanks for reporting.

The file is part of a multi-file series but does not follow the CZI multi-file naming schema (section 3.5.1 of the CZI specification). The current version of czifile is not os.Path aware, so it does not attempt to parse the file name. Since multi-file handling was never fully implemented, it's probably best to remove all of it.