import io


def _encode_image(image_array, fmt):
  """encodes an (numpy) image array to string.

  Args:
    image_array: (numpy) image array
    fmt: image format to use

  Returns:
    encoded image string
  """
  from PIL import Image  # pylint: disable=g-import-not-at-top
  pil_image = Image.fromarray(image_array)
  image_io = io.BytesIO()
  pil_image.save(image_io, format=fmt)
  return image_io.getvalue()