MCPcopy
hub / github.com/BIT-DataLab/Edit-Banana / merge_with_text_xml

Method merge_with_text_xml

modules/xml_merger.py:527–635  ·  view source on GitHub ↗

合并图形XML和文字XML 这是最常用的合并场景:SAM3提取的图形 + OCR提取的文字 Args: shape_xml_path: 图形XML路径 text_xml_path: 文字XML路径 output_path: 输出路径 image_path: 原始图片路径(用于获取尺寸) Returns: 输出文件路径

(self,
                            shape_xml_path: str,
                            text_xml_path: str,
                            output_path: str,
                            image_path: str = None)

Source from the content-addressed store, hash-verified

525 return output_path
526
527 def merge_with_text_xml(self,
528 shape_xml_path: str,
529 text_xml_path: str,
530 output_path: str,
531 image_path: str = None) -> str:
532 """
533 合并图形XML和文字XML
534
535 这是最常用的合并场景:SAM3提取的图形 + OCR提取的文字
536
537 Args:
538 shape_xml_path: 图形XML路径
539 text_xml_path: 文字XML路径
540 output_path: 输出路径
541 image_path: 原始图片路径(用于获取尺寸)
542
543 Returns:
544 输出文件路径
545 """
546 self._log("合并图形和文字XML")
547
548 fragments = []
549
550 # 解析图形XML
551 shape_tree = ET.parse(shape_xml_path)
552 shape_root = shape_tree.getroot()
553
554 # 获取画布尺寸
555 model = shape_root.find(".//mxGraphModel")
556 canvas_width = int(model.get("pageWidth", 800))
557 canvas_height = int(model.get("pageHeight", 600))
558
559 # 提取图形cells并判断层级
560 root_elem = shape_root.find(".//root")
561 for cell in list(root_elem):
562 cell_id = cell.get("id")
563 if cell_id in ["0", "1"]:
564 continue
565
566 cell_xml = ET.tostring(cell, encoding='unicode')
567 style = cell.get("style", "")
568
569 # 根据style判断层级
570 if "image=data:image" in style:
571 layer = LayerLevel.IMAGE.value
572 else:
573 layer = LayerLevel.BASIC_SHAPE.value
574
575 # 提取geometry信息用于排序
576 geom = cell.find("mxGeometry")
577 bbox = None
578 if geom is not None:
579 try:
580 bbox = BoundingBox(
581 x1=int(float(geom.get("x", 0))),
582 y1=int(float(geom.get("y", 0))),
583 x2=int(float(geom.get("x", 0))) + int(float(geom.get("width", 0))),
584 y2=int(float(geom.get("y", 0))) + int(float(geom.get("height", 0)))

Callers 1

merge_shape_and_textFunction · 0.95

Calls 6

processMethod · 0.95
BoundingBoxClass · 0.85
XMLFragmentClass · 0.85
ProcessingContextClass · 0.85
_logMethod · 0.80
findMethod · 0.45

Tested by

no test coverage detected