class_docstr: Load image file or files from provided path based on reader.
  If reader is not specified, this class automatically chooses readers
  based on the supported suffixes and in the following order:
  
      - User-specified reader at runtime when calling this loader.
      - User-specified reader in the constructor of `LoadImage`.
      - Readers from the last to the first in the registered list.
      - Current default readers: (nii, nii.gz -> NibabelReader), (png, jpg, bmp -> PILReader),
        (npz, npy -> NumpyReader), (nrrd -> NrrdReader), (DICOM file -> ITKReader).
  
  Please note that for png, jpg, bmp, and other 2D formats, readers by default swap axis 0 and 1 after
  loading the array with ``reverse_indexing`` set to ``True`` because the spatial axes definition
  for non-medical specific file formats is different from other common medical packages.
  
  See also:
  
      - tutorial: https://github.com/Project-MONAI/tutorials/blob/master/modules/load_medical_images.ipynb
class_docstr_tok: ["Load","image","file","or","files","from","provided","path","based","on","reader",".","If","reader","is","not","specified",",","this","class","automatically","chooses","readers","based","on","the","supported","suffixes","and","in","the","following","order",":","-","User-specified","reader","at","runtime","when","calling","this","loader",".","-","User-specified","reader","in","the","constructor","of","`","LoadImage","`",".","-","Readers","from","the","last","to","the","first","in","the","registered","list",".","-","Current","default","readers",":","(","nii",",","nii.gz","-",">","NibabelReader",")",",","(","png",",","jpg",",","bmp","-",">","PILReader",")",",","(","npz",",","npy","-",">","NumpyReader",")",",","(","nrrd","-",">","NrrdReader",")",",","(","DICOM","file","-",">","ITKReader",")",".","Please","note","that","for","png",",","jpg",",","bmp",",","and","other","2D","formats",",","readers","by","default","swap","axis","0","and","1","after","loading","the","array","with","``","reverse_indexing","``","set","to","``","True","``","because","the","spatial","axes","definition","for","non-medical","specific","file","formats","is","different","from","other","common","medical","packages",".","See","also",":","-","tutorial",":","https",":","\/\/github.com\/Project-MONAI\/tutorials\/blob\/master\/modules\/load_medical_images.ipynb"]
docstr: Load image file and metadata from the given filename(s).
  If `reader` is not specified, this class automatically chooses readers based on the
  reversed order of registered readers `self.readers`.
  
  Args:
      filename: path file or file-like object or a list of files.
          will save the filename to meta_data with key `filename_or_obj`.
          if provided a list of files, use the filename of first file to save,
          and will stack them together as multi-channels data.
          if provided directory path instead of file path, will treat it as
          DICOM images series and read.
      reader: runtime reader to load image file and metadata.
docstr_tok: ["Load","image","file","and","metadata","from","the","given","filename","(","s",")",".","If","`","reader","`","is","not","specified",",","this","class","automatically","chooses","readers","based","on","the","reversed","order","of","registered","readers","`","self.readers","`",".","Args",":","filename",":","path","file","or","file-like","object","or","a","list","of","files",".","will","save","the","filename","to","meta_data","with","key","`","filename_or_obj","`",".","if","provided","a","list","of","files",",","use","the","filename","of","first","file","to","save",",","and","will","stack","them","together","as","multi-channels","data",".","if","provided","directory","path","instead","of","file","path",",","will","treat","it","as","DICOM","images","series","and","read",".","reader",":","runtime","reader","to","load","image","file","and","metadata","."]
code: def __call__(
      self,
      filename: Sequence[PathLike] | PathLike,
      reader: ImageReader | None = None,
  ):
      """
      Load image file and metadata from the given filename(s).
      If `reader` is not specified, this class automatically chooses readers based on the
      reversed order of registered readers `self.readers`.
  
      Args:
          filename: path file or file-like object or a list of files.
              will save the filename to meta_data with key `filename_or_obj`.
              if provided a list of files, use the filename of first file to save,
              and will stack them together as multi-channels data.
              if provided directory path instead of file path, will treat it as
              DICOM images series and read.
          reader: runtime reader to load image file and metadata.
  
      """
      filename = tuple(
          f"{Path(s).expanduser()}" if self.expanduser else s
          for s in ensure_tuple(filename)  # allow Path objects
      )
      img, err = None, []
      if reader is not None:
          img = reader.read(filename)  # runtime specified reader
      else:
          for reader in self.readers[::-1]:
              if (
                  self.auto_select
              ):  # rely on the filename extension to choose the reader
                  if reader.verify_suffix(filename):
                      img = reader.read(filename)
                      break
              else:  # try the user designated readers
                  try:
                      img = reader.read(filename)
                  except Exception as e:
                      err.append(traceback.format_exc())
                      logging.getLogger(self.__class__.__name__).debug(
                          e, exc_info=True
                      )
                      logging.getLogger(self.__class__.__name__).info(
                          f"{reader.__class__.__name__}: unable to load {filename}.\n"
                      )
                  else:
                      err = []
                      break
  
      if img is None or reader is None:
          if isinstance(filename, Sequence) and len(filename) == 1:
              filename = filename[0]
          msg = "\n".join([f"{e}" for e in err])
          raise RuntimeError(
              f"{self.__class__.__name__} cannot find a suitable reader for file: {filename}.\n"
              "    Please install the reader libraries, see also the installation instructions:\n"
              "    https://docs.monai.io/en/latest/installation.html#installing-the-recommended-dependencies.\n"
              f"   The current registered: {self.readers}.\n{msg}"
          )
  
      img_array: NdarrayOrTensor
      img_array, meta_data = reader.get_data(img)
      img_array = convert_to_dst_type(
          img_array, dst=img_array, dtype=self.dtype
      )[0]
      if not isinstance(meta_data, dict):
          raise ValueError(
              f"`meta_data` must be a dict, got type {type(meta_data)}."
          )
      # make sure all elements in metadata are little endian
      meta_data = switch_endianness(meta_data, "<")
  
      meta_data[
          Key.FILENAME_OR_OBJ
      ] = f"{ensure_tuple(filename)[0]}"  # Path obj should be strings for data loader
      img = MetaTensor.ensure_torch_and_prune_meta(
          img_array,
          meta_data,
          self.simple_keys,
          pattern=self.pattern,
          sep=self.sep,
      )
      if self.ensure_channel_first:
          img = EnsureChannelFirst()(img)
      if self.image_only:
          return img
      return img, img.meta if isinstance(img, MetaTensor) else meta_data
  
imports: from __future__ import annotations
  import inspect
  import logging
  import sys
  import traceback
  import warnings
  from collections.abc import Sequence
  from pathlib import Path
  from pydoc import locate
  from typing import Callable
  import numpy
  import torch
  from monai.config import DtypeLike, NdarrayOrTensor, PathLike
  from monai.data import image_writer
  from monai.data.folder_layout import FolderLayout, FolderLayoutBase, default_name_formatter
  from monai.data.image_reader import ImageReader, ITKReader, NibabelReader, NrrdReader, NumpyReader, PILReader, PydicomReader
  from monai.data.meta_tensor import MetaTensor
  from monai.data.utils import is_no_channel
  from monai.transforms.transform import Transform
  from monai.transforms.utility.array import EnsureChannelFirst
  from monai.utils import GridSamplePadMode
  from monai.utils import ImageMetaKey
  from monai.utils import OptionalImportError, convert_to_dst_type, ensure_tuple, look_up_option, optional_import
label_desc: Use image node_id 3 for calling the LoadImage obj's underlying member method code with example usage: obj.__call__(filename, reader) and returns: img,  unknown, img