Skip to content

Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.

feat: optimize image handling by introducing caching for standard vector placeholders and ensuring single write for images

myhloli6d05b8895ac7
diff --git a/mineru/backend/utils/html_image_utils.py b/mineru/backend/utils/html_image_utils.py
index 3f7f6c4a..777c909e 100644
--- a/mineru/backend/utils/html_image_utils.py
+++ b/mineru/backend/utils/html_image_utils.py
@@ -18,7 +18,6 @@ def _normalize_image_extension(fmt: str) -> str:
def _save_inline_vector_image(
- b64_data_uri: str,
img_bytes: bytes,
image_writer,
fmt: str,
@@ -48,11 +47,29 @@ def _save_inline_vector_image(
logger.warning(f"Failed to decode converted vector image on page {page_index}: {exc}")
return None
- img_path = f"{str_sha256(b64_data_uri)}.{rendered_fmt}"
- image_writer.write(img_path, rendered_bytes)
+ img_path = f"{str_sha256(rendered_data_uri)}.{rendered_fmt}"
+ _write_image_once(image_writer, img_path, rendered_bytes)
return img_path
+def _write_image_once(image_writer, img_path: str, img_bytes: bytes) -> None:
+ """同一 writer 生命周期内同一路径只写一次,便于复用标准占位图。"""
+ written_paths = getattr(image_writer, "_mineru_written_image_paths", None)
+ if written_paths is None:
+ written_paths = set()
+ try:
+ setattr(image_writer, "_mineru_written_image_paths", written_paths)
+ except Exception:
+ image_writer.write(img_path, img_bytes)
+ return
+
+ if img_path in written_paths:
+ return
+
+ image_writer.write(img_path, img_bytes)
+ written_paths.add(img_path)
+
+
def save_base64_image(b64_data_uri: str, image_writer, page_index: int):
"""Persist a data-URI image via image_writer and return a relative path."""
if not image_writer:
@@ -73,7 +90,6 @@ def save_base64_image(b64_data_uri: str, image_writer, page_index: int):
if is_vector_image_part(content_type=f"image/{raw_fmt.lower()}"):
return _save_inline_vector_image(
- b64_data_uri,
img_bytes,
image_writer,
raw_fmt,
@@ -81,7 +97,7 @@ def save_base64_image(b64_data_uri: str, image_writer, page_index: int):
)
img_path = f"{str_sha256(b64_data_uri)}.{fmt}"
- image_writer.write(img_path, img_bytes)
+ _write_image_once(image_writer, img_path, img_bytes)
return img_path
diff --git a/mineru/backend/utils/office_image.py b/mineru/backend/utils/office_image.py
index 5a3ecd0f..79e79441 100644
--- a/mineru/backend/utils/office_image.py
+++ b/mineru/backend/utils/office_image.py
@@ -1,6 +1,8 @@
# Copyright (c) Opendatalab. All rights reserved.
+from functools import lru_cache
from io import BytesIO
from pathlib import PurePosixPath
+from typing import Final
from PIL import Image, ImageDraw, ImageFont, UnidentifiedImageError
from loguru import logger
@@ -21,6 +23,12 @@ VECTOR_IMAGE_CONTENT_TYPES = frozenset(
}
)
PIL_IMAGE_LOAD_ERRORS = (UnidentifiedImageError, OSError, SyntaxError)
+STANDARD_VECTOR_PLACEHOLDER_SIZE: Final = (320, 180)
+STANDARD_VECTOR_PLACEHOLDER_LINES: Final = (
+ "WMF/EMF placeholder",
+ "Use Windows to parse",
+ "the original image",
+)
def is_vector_image(pil_image: Image.Image) -> bool:
@@ -119,6 +127,21 @@ def create_text_placeholder(
return placeholder
+@lru_cache(maxsize=1)
+def _standard_vector_placeholder_data_uri() -> str:
+ """生成并缓存标准 WMF/EMF 占位图,避免每张矢量图重复绘制。"""
+ placeholder = create_text_placeholder(
+ STANDARD_VECTOR_PLACEHOLDER_SIZE,
+ list(STANDARD_VECTOR_PLACEHOLDER_LINES),
+ )
+ return image_to_b64str(placeholder, image_format="JPEG")
+
+
+def get_standard_vector_placeholder_data_uri() -> str:
+ """返回标准 WMF/EMF 占位图 data URI,供 Office 各格式复用。"""
+ return _standard_vector_placeholder_data_uri()
+
+
def serialize_vector_image_with_placeholder(
pil_image: Image.Image, image_format_override: str | None = None
) -> str:
@@ -134,22 +157,12 @@ def serialize_vector_image_with_placeholder(
logger.warning(
f"Failed to render {image_format} image: {e}, size: {pil_image.size}. Using placeholder instead."
)
- placeholder_lines = [
- f"{image_format} placeholder",
- "Windows rendering failed",
- ]
else:
logger.warning(
f"Skipping {image_format} image on non-Windows environment, size: {pil_image.size}"
)
- placeholder_lines = [
- f"{image_format} placeholder",
- "Use Windows to parse",
- "the original image",
- ]
- placeholder = create_text_placeholder(pil_image.size, placeholder_lines)
- return image_to_b64str(placeholder, image_format="JPEG")
+ return get_standard_vector_placeholder_data_uri()
def serialize_vector_part_with_placeholder(
@@ -160,17 +173,9 @@ def serialize_vector_part_with_placeholder(
image_format = _vector_image_format_label(part_name, content_type)
logger.warning(
f"Skipping {image_format} image part before Pillow load, "
- f"part_name={part_name}, content_type={content_type}"
+ f"part_name={part_name}, content_type={content_type}, requested_size={size}"
)
- placeholder = create_text_placeholder(
- size,
- [
- f"{image_format} placeholder",
- "Use Windows to parse",
- "the original image",
- ],
- )
- return image_to_b64str(placeholder, image_format="JPEG")
+ return get_standard_vector_placeholder_data_uri()
def serialize_office_image(