code: def _encode_image_from_nparray(image_array: np.ndarray) -> bytes:
      image = Image.fromarray(image_array)
      encoded_buffer = io.BytesIO()
      image.save(encoded_buffer, format="PNG")
      return encoded_buffer.getvalue()
  
file_path: model-analysis/tensorflow_model_analysis/metrics/semantic_segmentation_confusion_matrix_metrics_test.py
imports: import io
  from absl.testing import absltest
  from absl.testing import parameterized
  import apache_beam
  from apache_beam.testing import util
  import numpy
  from PIL import Image
  import tensorflow_model_analysis
  from tensorflow_model_analysis import constants
  from tensorflow_model_analysis.contrib.aggregates import binary_confusion_matrices
  from tensorflow_model_analysis.metrics import metric_types
  from google.protobuf import text_format
label_desc: Use image node_id 1 for calling a global function with example usage: _encode_image_from_nparray(image_array) and returns: encoded_buffer