code: def array_to_img(x, scale=True):
      
      from PIL import Image
      if x.ndim != 3:
          raise Exception('Unsupported shape : ', str(x.shape), '. Need (channels, width, height)')
      if scale:
          x += max(-np.min(x), 0)
          x /= np.max(x)
          x *= 255
      if x.shape[0] == 3:
          # RGB
          if x.dtype != 'uint8':
              x = x.astype('uint8')
          return Image.fromarray(x.astype('uint8'), 'RGB')
      elif x.shape[0] == 1:
          # grayscale
          if x.dtype != 'uint8':
              x = x.astype('uint8')
          return Image.fromarray(x.reshape(x.shape[1], x.shape[2]), 'L')
      else:
          raise Exception('Unsupported channel number: ', x.shape[0])
docs: x should be shape (channels, width, height)