code: def write_palette_png(
      data: np.array, filename: PathLike, palette: np.ndarray = None
  ):
      """Writes grayscale data as pngs to path using a fixed palette (e.g. for segmentations)."""
      assert data.ndim == 3, data.shape
      height, width, channels = data.shape
      assert channels == 1, "Must be grayscale"
  
      if data.dtype in [np.uint16, np.uint32, np.uint64]:
          max_value = np.amax(data)
          if max_value > 255:
              logger.warning(
                  "max_value %d exceeds uint bounds for %s.",
                  max_value,
                  filename,
              )
          data = data.astype(np.uint8)
      elif data.dtype == np.uint8:
          pass
      else:
          raise NotImplementedError(f"Cannot handle {data.dtype}.")
  
      if palette is None:
          palette = plotting.hls_palette(np.max(data) + 1)
  
      w = png.Writer(
          width=width, height=height, palette=palette, bitdepth=8
      )
      with gopen(filename, "wb") as fp:
          w.write(fp, data[:, :, 0])
  
imports: import contextlib
  import functools
  import logging
  import json
  import multiprocessing
  import pickle
  from typing import Any, Dict
  from etils import epath
  import imageio
  import numpy
  import png
  import tensorflow
  from kubric import plotting
  from kubric.kubric_typing import PathLike
label_desc: Use image node_id 7 for calling a global function with example usage: write_palette_png(data, filename, palette) without return types