docstr: Save an ndimage in the ImageResource.
  
  Parameters
  ----------
  ndimage : ArrayLike, List[ArrayLike]
      The image data that will be written to the file. It is either a
      single image, a batch of images, or a list of images.
  is_batch : bool
      If True, the provided ndimage is a batch of images. If False (default), the
      provided ndimage is a single image. If the provided ndimage is a list of images,
      this parameter has no effect.
  params : List[int]
      A list of parameters that will be passed to OpenCVs imwrite or
      imwritemulti functions. Possible values are documented in the
      `OpenCV documentation
      <https://docs.opencv.org/4.x/d4/da8/group__imgcodecs.html#gabbc7ef1aa2edfaa87772f1202d67e0ce>`_.
  
  Returns
  -------
  encoded_image : bytes, None
      If the ImageResource is ``"<bytes>"`` the call to write returns the
      encoded image as a bytes string. Otherwise it returns None.
docstr_tok: ["Save","an","ndimage","in","the","ImageResource",".","Parameters","--","--","--","--","--","ndimage",":","ArrayLike",",","List","[","ArrayLike","]","The","image","data","that","will","be","written","to","the","file",".","It","is","either","a","single","image",",","a","batch","of","images",",","or","a","list","of","images",".","is_batch",":","bool","If","True",",","the","provided","ndimage","is","a","batch","of","images",".","If","False","(","default",")",",","the","provided","ndimage","is","a","single","image",".","If","the","provided","ndimage","is","a","list","of","images",",","this","parameter","has","no","effect",".","params",":","List","[","int","]","A","list","of","parameters","that","will","be","passed","to","OpenCVs","imwrite","or","imwritemulti","functions",".","Possible","values","are","documented","in","the","`","OpenCV","documentation","<","https",":","\/\/docs.opencv.org\/4.x\/d4\/da8\/group__imgcodecs.html","#","gabbc7ef1aa2edfaa87772f1202d67e0ce",">","`","_",".","Returns","--","--","--","-","encoded_image",":","bytes",",","None","If","the","ImageResource","is","``","``","<","bytes",">","''","``","the","call","to","write","returns","the","encoded","image","as","a","bytes","string",".","Otherwise","it","returns","None","."]
code: def write(
      self,
      ndimage: Union[ArrayLike, List[ArrayLike]],
      is_batch: bool = False,
      params: List[int] = None,
  ) -> Optional[bytes]:
      """Save an ndimage in the ImageResource.
  
      Parameters
      ----------
      ndimage : ArrayLike, List[ArrayLike]
          The image data that will be written to the file. It is either a
          single image, a batch of images, or a list of images.
      is_batch : bool
          If True, the provided ndimage is a batch of images. If False (default), the
          provided ndimage is a single image. If the provided ndimage is a list of images,
          this parameter has no effect.
      params : List[int]
          A list of parameters that will be passed to OpenCVs imwrite or
          imwritemulti functions. Possible values are documented in the
          `OpenCV documentation
          <https://docs.opencv.org/4.x/d4/da8/group__imgcodecs.html#gabbc7ef1aa2edfaa87772f1202d67e0ce>`_.
  
      Returns
      -------
      encoded_image : bytes, None
          If the ImageResource is ``"<bytes>"`` the call to write returns the
          encoded image as a bytes string. Otherwise it returns None.
  
      """
  
      if isinstance(ndimage, list):
          ndimage = np.stack(ndimage, axis=0)
      elif not is_batch:
          ndimage = ndimage[None, ...]
  
      if ndimage[0].ndim == 2:
          n_channels = 1
      else:
          n_channels = ndimage[0].shape[-1]
  
      if n_channels == 1:
          ndimage_cv2 = [x for x in ndimage]
      elif n_channels == 4:
          ndimage_cv2 = [
              cv2.cvtColor(x, cv2.COLOR_RGBA2BGRA) for x in ndimage
          ]
      else:
          ndimage_cv2 = [
              cv2.cvtColor(x, cv2.COLOR_RGB2BGR) for x in ndimage
          ]
  
      retval = cv2.imwritemulti(self.file_handle, ndimage_cv2, params)
  
      if retval is False:
          # not sure what scenario would trigger this, but
          # it can occur theoretically.
          raise IOError("OpenCV failed to write.")  # pragma: no cover
  
      if self.request._uri_type == URI_BYTES:
          return Path(self.file_handle).read_bytes()
  
file_path: imageio/imageio/plugins/opencv.py
imports: import warnings
  from pathlib import Path
  from typing import Any, Dict, List, Optional, Union
  import cv2
  import numpy
  from ..core import Request
  from ..core.request import URI_BYTES, InitializationError, IOMode
  from ..core.v3_plugin_api import ImageProperties, PluginV3
  from ..typing import ArrayLike
label_desc: Use image node_id 4 for calling the OpenCVPlugin obj's underlying member method code with example usage: obj.write(ndimage, is_batch, params) and returns: Path