code: def create_split_pages_dict_structure(
      split_pages, save_to_output_path=None
  ):
      """
      From a list of split pages <split_pages> with tuples containing
      (base double page, (split pages from split_page_texts), (split page images)) form a new page dict structure and new
      XML element structure.
      Return tuple (new XML element tree object, new XML element root, new pages dict with split pages)
      """
      if save_to_output_path:
          output_dir = os.path.dirname(save_to_output_path)
          output_fname = os.path.basename(save_to_output_path)
  
          if not output_fname.endswith(".xml"):
              raise ValueError(
                  "file in save_to_output_path '%s' must end with '.xml'"
                  % save_to_output_path
              )
  
          dot_idx = output_fname.rindex(".")
          basename = output_fname[:dot_idx]
      else:
          output_dir = None
          basename = None
  
      new_root = ET.Element("pdf2xml", {"producer": "pdftabextract"})
  
      page_num = 1
      pages = OrderedDict()
      for base_p, texts_pair, image_pair in split_pages:
          base_page_elem = base_p["xmlnode"]
  
          for (texts, p_w, p_h), img in zip(texts_pair, image_pair):
              # deep copy text boxes
              new_texts = [deepcopy(t) for t in texts]
  
              # create page dict
              new_p_dict = {
                  "number": page_num,
                  "width": int(round(p_w)),
                  "height": int(round(p_h)),
                  "texts": new_texts,
              }
  
              # create <page>
              new_page_elem = ET.Element(
                  "page",
                  {
                      "number": str(page_num),
                      "position": "absolute",
                      "top": "0",
                      "left": "0",
                      "width": str(new_p_dict["width"]),
                      "height": str(new_p_dict["height"]),
                  },
              )
  
              # add <fontspec>s
              new_page_elem.extend(
                  deepcopy(base_page_elem.findall("fontspec"))
              )
  
              # save and add <image>
              if save_to_output_path:
                  img_h, img_w = img.shape[:2]
                  imgfile = "%s_%d.png" % (basename, page_num)
                  cv2.imwrite(os.path.join(output_dir, imgfile), img)
                  image_elem = ET.Element(
                      "image",
                      {
                          "top": "0",
                          "left": "0",
                          "width": str(int(round(img_w))),
                          "height": str(int(round(img_h))),
                          "src": imgfile,
                      },
                  )
                  new_page_elem.append(image_elem)
              else:
                  imgfile = None
  
              # add <text>s
              new_page_elem.extend([t["xmlnode"] for t in new_texts])
  
              # add to root
              new_root.append(new_page_elem)
  
              new_p_dict["image"] = imgfile
              new_p_dict["xmlnode"] = new_page_elem
              pages[page_num] = new_p_dict
  
              page_num += 1
  
      new_tree = ET.ElementTree(new_root)
  
      if save_to_output_path:
          new_tree.write(save_to_output_path)
  
      return new_tree, new_root, pages
  
imports: import xml.etree.ElementTree
  from collections import OrderedDict
  from copy import deepcopy
  import os
  import cv2
  from .common import DIRECTION_HORIZONTAL, DIRECTION_VERTICAL, update_text_dict_pos
label_desc: Use image node_id 2 for calling a global function with example usage: create_split_pages_dict_structure(split_pages, save_to_output_path) and returns: new_tree,  new_root,  pages