code: def create_image_summary(name, val):
      """
      Args:
          name(str):
          val(np.ndarray): 4D tensor of NHWC. assume RGB if C==3.
              Can be either float or uint8. Range has to be [0,255].
  
      Returns:
          tf.Summary:
      """
      assert isinstance(name, six.string_types), type(name)
      n, h, w, c = val.shape
      val = val.astype("uint8")
      s = tf.Summary()
      imparams = [cv2.IMWRITE_PNG_COMPRESSION, 9]
      for k in range(n):
          arr = val[k]
          # CV2 will only write correctly in BGR chanel order
          if c == 3:
              arr = cv2.cvtColor(arr, cv2.COLOR_RGB2BGR)
          elif c == 4:
              arr = cv2.cvtColor(arr, cv2.COLOR_RGBA2BGRA)
          tag = name if n == 1 else "{}/{}".format(name, k)
          retval, img_str = cv2.imencode(".png", arr, imparams)
          if not retval:
              # Encoding has failed.
              continue
          img_str = img_str.tostring()
  
          img = tf.Summary.Image()
          img.height = h
          img.width = w
          # 1 - grayscale 3 - RGB 4 - RGBA
          img.colorspace = c
          img.encoded_image_string = img_str
          s.value.add(tag=tag, image=img)
      return s
  
file_path: tensorpack/tensorpack/tfutils/summary.py
imports: import re
  from contextlib import contextmanager
  import six
  from six.moves import range
  from tensorflow.python.training import moving_averages
  from ..compat import tfv1
  from ..utils import logger
  from ..utils.argtools import graph_memoized
  from ..utils.naming import MOVING_SUMMARY_OPS_KEY
  from .scope_utils import cached_name_scope
  from .symbolic_functions import rms
  from .tower import get_current_tower_context
label_desc: Use image node_id 4 for calling a global function with example usage: create_image_summary(name, val) and returns: s