code: def reorder_img(img, resample=None):
      """Return an image with the affine diagonal (by permuting axes).
  
      The orientation of the new image will be RAS (Right, Anterior, Superior).
      If it is impossible to get xyz ordering by permuting the axes, a
      'ValueError' is raised.
  
      Parameters
      ----------
      img : Niimg-like object
          See :ref:`extracting_data`.
          Image to reorder.
  
      resample : None or string in {'continuous', 'linear', 'nearest'}, optional
          If resample is None (default), no resampling is performed, the
          axes are only permuted.
          Otherwise resampling is performed and 'resample' will
          be passed as the 'interpolation' argument into
          resample_img.
  
      """
      from .image import new_img_like
  
      img = _utils.check_niimg(img)
      # The copy is needed in order not to modify the input img affine
      # see https://github.com/nilearn/nilearn/issues/325 for a concrete bug
      affine = img.affine.copy()
      A, b = to_matrix_vector(affine)
  
      if not np.all((np.abs(A) > 0.001).sum(axis=0) == 1):
          if resample is None:
              raise ValueError(
                  "Cannot reorder the axes: "
                  "the image affine contains rotations"
              )
  
          # Identify the voxel size using a QR decomposition of the
          # affine
          Q, R = np.linalg.qr(affine[:3, :3])
          target_affine = np.diag(
              np.abs(np.diag(R))[np.abs(Q).argmax(axis=1)]
          )
          return resample_img(
              img, target_affine=target_affine, interpolation=resample
          )
  
      axis_numbers = np.argmax(np.abs(A), axis=0)
      data = _get_data(img)
      while not np.all(np.sort(axis_numbers) == axis_numbers):
          first_inversion = np.argmax(np.diff(axis_numbers) < 0)
          axis1 = first_inversion + 1
          axis2 = first_inversion
          data = np.swapaxes(data, axis1, axis2)
          order = np.array((0, 1, 2, 3))
          order[axis1] = axis2
          order[axis2] = axis1
          affine = affine.T[order].T
          A, b = to_matrix_vector(affine)
          axis_numbers = np.argmax(np.abs(A), axis=0)
  
      # Now make sure the affine is positive
      pixdim = np.diag(A).copy()
      if pixdim[0] < 0:
          b[0] = b[0] + pixdim[0] * (data.shape[0] - 1)
          pixdim[0] = -pixdim[0]
          slice1 = slice(None, None, -1)
      else:
          slice1 = slice(None, None, None)
      if pixdim[1] < 0:
          b[1] = b[1] + pixdim[1] * (data.shape[1] - 1)
          pixdim[1] = -pixdim[1]
          slice2 = slice(None, None, -1)
      else:
          slice2 = slice(None, None, None)
      if pixdim[2] < 0:
          b[2] = b[2] + pixdim[2] * (data.shape[2] - 1)
          pixdim[2] = -pixdim[2]
          slice3 = slice(None, None, -1)
      else:
          slice3 = slice(None, None, None)
      data = data[slice1, slice2, slice3]
      affine = from_matrix_vector(np.diag(pixdim), b)
  
      return new_img_like(img, data, affine)
  
file_path: nilearn/nilearn/image/resampling.py
imports: import numbers
  import warnings
  import numpy
  from scipy import linalg
  from scipy.ndimage import affine_transform, find_objects
  from ..None import _utils
  from .._utils import stringify_path
  from .._utils.niimg import _get_data
  from .image import copy_img, crop_img
label_desc: Use image node_id 9 for calling a global function with example usage: reorder_img(img, resample) and returns: new_img_like, resample_img