code: def write_png(data: np.array, filename: PathLike) -> None:
      """Writes data as a png file (and convert datatypes if necessary)."""
  
      if data.dtype in [np.uint32, np.uint64]:
          max_value = np.amax(data)
          if max_value > 65535:
              logger.warning(
                  "max_value %d exceeds uint16 bounds for %s.",
                  max_value,
                  filename,
              )
              raise ValueError(
                  f"max value of {max_value} exceeds uint16 bounds for {filename}"
              )
          data = data.astype(np.uint16)
      elif data.dtype in [np.float32, np.float64]:
          min_value = np.amin(data)
          max_value = np.amax(data)
          if min_value < 0.0 or max_value > 1.0:
              raise ValueError(
                  f"Values need to be in range [0, 1] but got [{min_value}, {max_value}] "
                  f"for {filename}"
              )
          data = (data * 65535).astype(np.uint16)
      elif data.dtype in [np.uint8, np.uint16]:
          pass
      else:
          raise NotImplementedError(f"Cannot handle {data.dtype}.")
  
      bitdepth = 8 if data.dtype == np.uint8 else 16
  
      assert data.ndim == 3, data.shape
      height, width, channels = data.shape
      greyscale = channels == 1
      alpha = channels == 4
      w = png.Writer(
          width=width,
          height=height,
          greyscale=greyscale,
          bitdepth=bitdepth,
          alpha=alpha,
      )
  
      if channels == 2:
          # Pad two-channel images with a zero channel.
          data = np.concatenate(
              [data, np.zeros_like(data[:, :, :1])], axis=-1
          )
  
      # pypng expects 2d arrays
      # see https://pypng.readthedocs.io/en/latest/ex.html#reshaping
      data = data.reshape(height, -1)
      with gopen(filename, "wb") as fp:
          w.write(fp, data)
  
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 6 for calling a global function with example usage: write_png(data, filename) without return types