code: def compare_images(im1, im2, threshold=1, use_vtk=True):
      """Compare two different images of the same size.
  
      Parameters
      ----------
      im1 : str | numpy.ndarray | vtkRenderWindow | vtkImageData
          Render window, numpy array representing the output of a render
          window, or ``vtkImageData``.
  
      im2 : str | numpy.ndarray | vtkRenderWindow | vtkImageData
          Render window, numpy array representing the output of a render
          window, or ``vtkImageData``.
  
      threshold : int, default: 1
          Threshold tolerance for pixel differences.  This should be
          greater than 0, otherwise it will always return an error, even
          on identical images.
  
      use_vtk : bool, default: True
          When disabled, computes the mean pixel error over the entire
          image using numpy.  The difference between pixel is calculated
          for each RGB channel, summed, and then divided by the number
          of pixels.  This is faster than using
          ``vtk.vtkImageDifference`` but potentially less accurate.
  
      Returns
      -------
      float
          Total error between the images if using ``use_vtk=True``, and
          the mean pixel error when ``use_vtk=False``.
  
      Examples
      --------
      Compare two active plotters.
  
      >>> import pyvista as pv
      >>> pl1 = pv.Plotter()
      >>> _ = pl1.add_mesh(pv.Sphere(), smooth_shading=True)
      >>> pl2 = pv.Plotter()
      >>> _ = pl2.add_mesh(pv.Sphere(), smooth_shading=False)
      >>> error = pv.compare_images(pl1, pl2)
  
      Compare images from file.
  
      >>> import pyvista as pv
      >>> img1 = pv.read('img1.png')  # doctest:+SKIP
      >>> img2 = pv.read('img2.png')  # doctest:+SKIP
      >>> pv.compare_images(img1, img2)  # doctest:+SKIP
  
      """
      from pyvista import ImageData, Plotter, read, wrap
  
      def to_img(img):  # numpydoc ignore=GL08
          if isinstance(img, ImageData):  # pragma: no cover
              return img
          elif isinstance(img, _vtk.vtkImageData):
              return wrap(img)
          elif isinstance(img, str):
              return read(img)
          elif isinstance(img, np.ndarray):
              return wrap_image_array(img)
          elif isinstance(img, Plotter):
              if (
                  img._first_time
              ):  # must be rendered first else segfault
                  img._on_first_render_request()
                  img.render()
              if img.render_window is None:
                  raise RuntimeError(
                      "Unable to extract image from Plotter as it has already been closed."
                  )
              return image_from_window(
                  img.render_window, True, ignore_alpha=True
              )
          else:
              raise TypeError(
                  f"Unsupported data type {type(img)}.  Should be "
                  "Either a np.ndarray, vtkRenderWindow, or vtkImageData"
              )
  
      im1 = remove_alpha(to_img(im1))
      im2 = remove_alpha(to_img(im2))
  
      if im1.GetDimensions() != im2.GetDimensions():
          raise RuntimeError("Input images are not the same size.")
  
      if use_vtk:
          img_diff = _vtk.vtkImageDifference()
          img_diff.SetThreshold(threshold)
          img_diff.SetInputData(im1)
          img_diff.SetImageData(im2)
          img_diff.AllowShiftOff()  # vastly increases compute time when enabled
          # img_diff.AveragingOff()  # increases compute time
          img_diff.Update()
          return img_diff.GetThresholdedError()
  
      # otherwise, simply compute the mean pixel difference
      diff = np.abs(im1.point_data[0] - im2.point_data[0])
      return np.sum(diff) / im1.point_data[0].shape[0]
  
file_path: pyvista/pyvista/plotting/utilities/regression.py
imports: from typing import Optional, cast
  import numpy
  import pyvista
  from pyvista.core.utilities.arrays import point_array
  from pyvista.core.utilities.helpers import wrap
  from pyvista.plotting import _vtk
label_desc: Use image node_id 5 for calling a global function with example usage: compare_images(im1, im2, threshold, use_vtk) and returns: unknown, img_diff, img, wrap, read, wrap_image_array, image_from_window