code: def load_niimg(niimg, dtype=None):
      """Load a niimg, check if it is a nibabel SpatialImage and cast if needed.
  
      Parameters
      ----------
      niimg: Niimg-like object
          See :ref:`extracting_data`.
          Image to load.
  
      dtype: {dtype, "auto"}
          Data type toward which the data should be converted. If "auto", the
          data will be converted to int32 if dtype is discrete and float32 if it
          is continuous.
  
      Returns
      -------
      img: image
          A loaded image object.
      """
      from ..image import new_img_like  # avoid circular imports
  
      niimg = stringify_path(niimg)
      if isinstance(niimg, str):
          # data is a filename, we load it
          niimg = nibabel.load(niimg)
      elif not isinstance(niimg, nibabel.spatialimages.SpatialImage):
          raise TypeError(
              "Data given cannot be loaded because it is"
              " not compatible with nibabel format:\n"
              + _repr_niimgs(niimg, shorten=True)
          )
  
      dtype = _get_target_dtype(_get_data(niimg).dtype, dtype)
  
      if dtype is not None:
          # Copyheader and set dtype in header if header exists
          if niimg.header is not None:
              niimg = new_img_like(
                  niimg,
                  _get_data(niimg).astype(dtype),
                  niimg.affine,
                  copy_header=True,
              )
              niimg.header.set_data_dtype(dtype)
          else:
              niimg = new_img_like(
                  niimg, _get_data(niimg).astype(dtype), niimg.affine
              )
  
      return niimg
  
file_path: nilearn/nilearn/_utils/niimg.py
imports: import collections.abc
  import copy
  import gc
  from pathlib import Path
  from warnings import warn
  import nibabel
  import numpy
  from .helpers import stringify_path
label_desc: Use image node_id 4 for calling a global function with example usage: load_niimg(niimg, dtype) and returns: niimg