code: def _normalize_palette(im, palette, info):
      """
      Normalizes the palette for image.
        - Sets the palette to the incoming palette, if provided.
        - Ensures that there's a palette for L mode images
        - Optimizes the palette if necessary/desired.
  
      :param im: Image object
      :param palette: bytes object containing the source palette, or ....
      :param info: encoderinfo
      :returns: Image object
      """
      source_palette = None
      if palette:
          # a bytes palette
          if isinstance(palette, (bytes, bytearray, list)):
              source_palette = bytearray(palette[:768])
          if isinstance(palette, ImagePalette.ImagePalette):
              source_palette = bytearray(
                  itertools.chain.from_iterable(
                      zip(
                          palette.palette[:256],
                          palette.palette[256:512],
                          palette.palette[512:768],
                      )
                  )
              )
  
      if im.mode == "P":
          if not source_palette:
              source_palette = im.im.getpalette("RGB")[:768]
      else:  # L-mode
          if not source_palette:
              source_palette = bytearray(i // 3 for i in range(768))
          im.palette = ImagePalette.ImagePalette(
              "RGB", palette=source_palette
          )
  
      used_palette_colors = _get_optimize(im, info)
      if used_palette_colors is not None:
          return im.remap_palette(used_palette_colors, source_palette)
  
      im.palette.palette = source_palette
      return im
  
imports: import itertools
  from .None import Image, ImageChops, ImageFile, ImagePalette, ImageSequence
  from ._binary import i8, i16le, o8, o16le
label_desc: Use image node_id 3 for calling a global function with example usage: _normalize_palette(im, palette, info) and returns: im, im