code: def encode_np_array(array: np.ndarray) -> dict:
      if config.PIL_AVAILABLE:
          import PIL.Image
      else:
          raise ImportError(
              "To support encoding images, please install 'Pillow'."
          )
  
      dtype = array.dtype
      dtype_byteorder = (
          dtype.byteorder
          if dtype.byteorder != "="
          else _NATIVE_BYTEORDER
      )
      dtype_kind = dtype.kind
      dtype_itemsize = dtype.itemsize
  
      dest_dtype = None
  
      # Multi-channel array case (only np.dtype("|u1") is allowed)
      if array.shape[2:]:
          if dtype_kind not in ["u", "i"]:
              raise TypeError(
                  f"Unsupported array dtype {dtype} for image encoding. Only {dest_dtype} is supported for multi-channel arrays."
              )
          dest_dtype = np.dtype("|u1")
          if dtype != dest_dtype:
              warnings.warn(
                  f"Downcasting array dtype {dtype} to {dest_dtype} to be compatible with 'Pillow'"
              )
      # Exact match
      elif dtype in _VALID_IMAGE_ARRAY_DTPYES:
          dest_dtype = dtype
      else:  # Downcast the type within the kind (np.can_cast(from_type, to_type, casting="same_kind") doesn't behave as expected, so do it manually)
          while dtype_itemsize >= 1:
              dtype_str = (
                  dtype_byteorder + dtype_kind + str(dtype_itemsize)
              )
              if np.dtype(dtype_str) in _VALID_IMAGE_ARRAY_DTPYES:
                  dest_dtype = np.dtype(dtype_str)
                  warnings.warn(
                      f"Downcasting array dtype {dtype} to {dest_dtype} to be compatible with 'Pillow'"
                  )
                  break
              else:
                  dtype_itemsize //= 2
          if dest_dtype is None:
              raise TypeError(
                  f"Cannot downcast dtype {dtype} to a valid image dtype. Valid image dtypes: {_VALID_IMAGE_ARRAY_DTPYES}"
              )
  
      image = PIL.Image.fromarray(array.astype(dest_dtype))
      return {"path": None, "bytes": image_to_bytes(image)}
  
imports: import os
  import sys
  import warnings
  from dataclasses import dataclass, field
  from io import BytesIO
  from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union
  import numpy
  import pyarrow
  from ..None import config
  from ..download.download_config import DownloadConfig
  from ..download.streaming_download_manager import xopen
  from ..table import array_cast
  from ..utils.file_utils import is_local_path
  from ..utils.py_utils import first_non_null_value, no_op_if_value_is_null, string_to_dict
label_desc: Use image node_id 4 for calling a global function with example usage: encode_np_array(array) and returns: dict