2525from .utils import is_supported_format
2626
2727if TYPE_CHECKING :
28- import cucim
2928 import itk # type: ignore
3029 import nibabel as nib
31- import openslide
3230 from nibabel .nifti1 import Nifti1Image
3331 from PIL import Image as PILImage
3432
35- has_itk = has_nib = has_pil = has_cim = has_osl = True
33+ has_itk = has_nib = has_pil = True
3634else :
3735 itk , has_itk = optional_import ("itk" , allow_namespace_pkg = True )
3836 nib , has_nib = optional_import ("nibabel" )
3937 Nifti1Image , _ = optional_import ("nibabel.nifti1" , name = "Nifti1Image" )
4038 PILImage , has_pil = optional_import ("PIL.Image" )
41- cucim , has_cim = optional_import ("cucim" )
42- openslide , has_osl = optional_import ("openslide" )
4339
4440__all__ = ["ImageReader" , "ITKReader" , "NibabelReader" , "NumpyReader" , "PILReader" , "WSIReader" ]
4541
@@ -670,13 +666,9 @@ def __init__(self, reader_lib: str = "OpenSlide"):
670666 super ().__init__ ()
671667 self .reader_lib = reader_lib .lower ()
672668 if self .reader_lib == "openslide" :
673- if has_osl :
674- self .wsi_reader = openslide .OpenSlide
669+ self .wsi_reader , * _ = optional_import ("openslide" , name = "OpenSlide" )
675670 elif self .reader_lib == "cucim" :
676- if has_cim and hasattr (cucim , "CuImage" ):
677- self .wsi_reader = cucim .CuImage
678- else :
679- raise ImportError ("Please check the cuCIM installation." )
671+ self .wsi_reader , * _ = optional_import ("cucim" , name = "CuImage" )
680672 else :
681673 raise ValueError ('`reader_lib` should be either "cuCIM" or "OpenSlide"' )
682674
@@ -699,11 +691,6 @@ def read(self, data: Union[Sequence[str], str, np.ndarray], **kwargs):
699691 data: file name or a list of file names to read.
700692
701693 """
702- if (self .reader_lib == "openslide" ) and (not has_osl ):
703- raise ImportError ("No module named 'openslide'" )
704- if (self .reader_lib == "cucim" ) and (not has_cim ):
705- raise ImportError ("No module named 'cucim'" )
706-
707694 img_ : List = []
708695
709696 filenames : Sequence [str ] = ensure_tuple (data )
0 commit comments