code: def _normalize_array(array, domain=(0, 1)):
      """Given an arbitrary rank-3 NumPy array, produce one representing an image.
  
      This ensures the resulting array has a dtype of uint8 and a domain of 0-255.
  
      Args:
        array: NumPy array representing the image
        domain: expected range of values in array,
          defaults to (0, 1), if explicitly set to None will use the array's
          own range of values and normalize them.
  
      Returns:
        normalized PIL.Image
      """
      # first copy the input so we're never mutating the user's data
      array = np.array(array)
      # squeeze helps both with batch=1 and B/W and PIL's mode inference
      array = np.squeeze(array)
      assert len(array.shape) <= 3
      assert np.issubdtype(array.dtype, np.number)
      assert not np.isnan(array).any()
  
      low, high = np.min(array), np.max(array)
      if domain is None:
          message = "No domain specified, normalizing from measured (~%.2f, ~%.2f)"
          log.debug(message, low, high)
          domain = (low, high)
  
      # clip values if domain was specified and array contains values outside of it
      if low < domain[0] or high > domain[1]:
          message = "Clipping domain from (~{:.2f}, ~{:.2f}) to (~{:.2f}, ~{:.2f})."
          log.info(message.format(low, high, domain[0], domain[1]))
          array = array.clip(*domain)
  
      min_value, max_value = (
          np.iinfo(np.uint8).min,
          np.iinfo(np.uint8).max,
      )  # 0, 255
      # convert signed to unsigned if needed
      if np.issubdtype(array.dtype, np.inexact):
          offset = domain[0]
          if offset != 0:
              array -= offset
              log.debug(
                  "Converting inexact array by subtracting -%.2f.",
                  offset,
              )
          if domain[0] != domain[1]:
              scalar = max_value / (domain[1] - domain[0])
              if scalar != 1:
                  array *= scalar
                  log.debug(
                      "Converting inexact array by scaling by %.2f.",
                      scalar,
                  )
  
      return array.clip(min_value, max_value).astype(np.uint8)
  
file_path: lucid/lucid/misc/io/serialize_array.py
imports: from __future__ import absolute_import, division, print_function
  import base64
  import logging
  import numpy
  import PIL.Image
  from io import BytesIO
label_desc: Use image node_id 1 for calling a global function with example usage: _normalize_array(array, domain) and returns: array