code: def equalize(image, mask=None):
      """
      Equalize the image histogram. This function applies a non-linear
      mapping to the input image, in order to create a uniform
      distribution of grayscale values in the output image.
  
      :param image: The image to equalize.
      :param mask: An optional mask.  If given, only the pixels selected by
                   the mask are included in the analysis.
      :return: An image.
      """
      if image.mode == "P":
          image = image.convert("RGB")
      h = image.histogram(mask)
      lut = []
      for b in range(0, len(h), 256):
          histo = [_f for _f in h[b : b + 256] if _f]
          if len(histo) <= 1:
              lut.extend(list(range(256)))
          else:
              step = (
                  functools.reduce(operator.add, histo) - histo[-1]
              ) // 255
              if not step:
                  lut.extend(list(range(256)))
              else:
                  n = step // 2
                  for i in range(256):
                      lut.append(n // step)
                      n = n + h[i + b]
      return _lut(image, lut)
  
imports: import functools
  import operator
  import re
  from .None import ExifTags, Image, ImagePalette
label_desc: Use image node_id 12 for calling a global function with example usage: equalize(image, mask) and returns: _lut