Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.
Python ai4sciencedocument-analysisdocxextract-datalayout-analysisocrparserpdfpdf-converterpdf-extractor-llmpdf-extractor-pretrainpdf-extractor-ragpdf-parserpptxpythonxlsx
Read Office image assets from sidecars
jinzhenjd1f72afc8020
diff --git a/mineru/doclib/server.py b/mineru/doclib/server.pyindex 515e4c97..bca51cb6 100644--- a/mineru/doclib/server.py+++ b/mineru/doclib/server.py@@ -3,7 +3,6 @@from __future__ import annotationsimport asyncio-import base64import osimport reimport signal@@ -1363,8 +1362,8 @@ class DoclibServer(AsyncDoclibInterface):raise InvalidRequestError("format_not_supported", "Office image output is only supported for image blocks.", "format")- image_bytes, _, _ = _span_image_bytes(image_span)- if image_bytes is None and image_span.image_path:+ image_bytes = None+ if image_span.image_path:image_dir = parse_image_sidecar_dir(self.state.data_dir, plan.sha256, plan.tier)candidate = resolve_image_sidecar_path(image_dir, image_span.image_path)if candidate is not None and candidate.is_file():@@ -1718,20 +1717,6 @@ def _iter_block_spans(block: Block) -> Iterator[Span]:yield from _iter_block_spans(child)-def _span_image_bytes(span: Span) -> tuple[bytes | None, str, str]:- # TODO: how to get image from office span?- if not span.image_base64:- return None, "png", "image/png"- match = re.match(r"^data:image/(?P<ext>[^;]+);base64,(?P<data>.+)$", span.image_base64, re.DOTALL)- if match is None:- return None, "png", "image/png"- ext = match.group("ext").lower()- try:- return base64.b64decode(match.group("data")), ext, _mime_type_for_ext(ext)- except Exception:- return None, ext, _mime_type_for_ext(ext)--_PIL_IMAGE_FORMATS: dict[ImageFormat, str] = {"jpeg": "JPEG","png": "PNG",@@ -1805,17 +1790,6 @@ def _write_temp_asset()-def _mime_type_for_ext(ext: str) -> str:- normalized = ext.lower().lstrip(".")- if normalized in {"jpg", "jpeg"}:- return "image/jpeg"- if normalized == "webp":- return "image/webp"- if normalized == "gif":- return "image/gif"- return "image/png"--def _render_progressive_markdown(pages: list[PageInfo],*,diff --git a/tests/unittest/test_doclib_cache_semantics.py b/tests/unittest/test_doclib_cache_semantics.pyindex 0beeebcd..1f32d97b 100644--- a/tests/unittest/test_doclib_cache_semantics.py+++ b/tests/unittest/test_doclib_cache_semantics.py@@ -3710,3 +3710,39 @@ def test_doclib_office_image_asset_transcodes_to_requested_format(tmp_path: Pathassert Path(asset.path).suffix == ".jpg"with Image.open(asset.path) as image:assert image.format == "JPEG"+++def test_doclib_office_image_asset_missing_sidecar_reports_asset_not_available(tmp_path: Path) -> None:+ sha256 = "f" * 64+ tier = "standard"+ image_path = "figures/missing.png"+ image_span = Span(type=ContentType.IMAGE, bbox=(1, 1, 20, 20), image_path=image_path)+ body = Block(+ index=0,+ type=BlockType.IMAGE_BODY,+ bbox=(1, 1, 20, 20),+ lines=[Line(bbox=(1, 1, 20, 20), spans=[image_span])],+ )+ image_block = Block(index=0, type=BlockType.IMAGE, bbox=(1, 1, 20, 20), blocks=[body])+ page = PageInfo(page_idx=0, page_size=(100, 100), para_blocks=[image_block], _backend="office")+ server = DoclibServer(SimpleNamespace(data_dir=str(tmp_path), db=None))+ plan = _ReadPlan(+ sha256=sha256,+ short_id="fffffff",+ tier=tier,+ page_range=None,+ after=None,+ locator="doc:fffffff/tier:standard/page:1/block:1",+ context=0,+ limit=30000,+ format="image",+ no_marker=False,+ image_format="png",+ target=ContentCursor(short_id="fffffff", tier=tier, page_no=1, block_no=1),+ )++ with pytest.raises(NotFoundError) as exc_info:+ asyncio.run(server._render_office_image_asset(plan, page))++ assert exc_info.value.code == "asset_not_available"+ assert exc_info.value.param == "locator"