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
fix: crop Middle JSON 0-1 bbox against rendered image size
jinzhenj317428b8e89b
diff --git a/mineru/doclib/server.py b/mineru/doclib/server.pyindex 558d0331..855895b5 100644--- a/mineru/doclib/server.py+++ b/mineru/doclib/server.py@@ -1444,7 +1444,7 @@ class DoclibServer(AsyncDoclibInterface):if block.bbox is None or _is_empty_bbox(block.bbox):raise InvalidRequestError("bbox_not_available", "Block bbox is not available for image output.", "locator")image_bytes = _transcode_image_bytes(- doc.crop_image(block.bbox, plan.target.page_no - 1, scale=doc.render_scale),+ doc.crop_image(block.bbox, plan.target.page_no - 1),plan.image_format,)width, height = _image_size_from_bytes(image_bytes)diff --git a/mineru/model/flash/pdf/document.py b/mineru/model/flash/pdf/document.pyindex 32fcf753..b753a32e 100644--- a/mineru/model/flash/pdf/document.py+++ b/mineru/model/flash/pdf/document.py@@ -367,11 +367,11 @@ class PDFDocument:return _page_to_image(page, scale, self.render_max_edge)# TODO: move- def crop_image(self, bbox: BBox, page_idx: int, *, scale: float = 2) -> bytes:- image = self.render_page(page_idx, scale=scale)+ def crop_image(self, bbox: BBox, page_idx: int) -> bytes:+ image = self.render_page(page_idx)crop = Nonetry:- crop = crop_pil_image(bbox, image.pil_image, scale=image.scale)+ crop = crop_pil_image(bbox, image.pil_image)return image_to_bytes(crop, image_format="JPEG")finally:if crop is not None:diff --git a/mineru/model/flash/pdf/geometry.py b/mineru/model/flash/pdf/geometry.pyindex 6cfda4d2..661bfc91 100644--- a/mineru/model/flash/pdf/geometry.py+++ b/mineru/model/flash/pdf/geometry.py@@ -59,11 +59,11 @@ def _bbox_area(bbox: BBox) -> float:return max(0.0, bbox[2] - bbox[0]) * max(0.0, bbox[3] - bbox[1])-def _normalize_bbox_to_thousandths(+def _normalize_bbox_to_unit(bbox: BBox,page_size: tuple[float, float],) -> list[float]:- """将绝对 bbox 转成千分位刻度,并保证舍入后的宽高至少各占一个刻度。"""+ """将绝对 bbox 归一化到 0-1 单位区间,并保证舍入后的宽高至少各占一个刻度。"""page_width, page_height = page_sizeticks = [diff --git a/mineru/model/flash/pdf/pipeline.py b/mineru/model/flash/pdf/pipeline.pyindex 98d226a2..64f2404e 100644--- a/mineru/model/flash/pdf/pipeline.py+++ b/mineru/model/flash/pdf/pipeline.py@@ -37,7 +37,7 @@ from .geometry import (_bbox_union_many,_clip_bbox,_coerce_bbox,- _normalize_bbox_to_thousandths,+ _normalize_bbox_to_unit,_rotate_bbox_from_upright,_rotate_bbox_to_upright,)@@ -915,7 +915,7 @@ def _normalize_output_block(normalized_type = block_type if block_type in _OUTPUT_BLOCK_TYPES else "text"if normalized_type not in {"image", "equation", "header", "footer"} and not content.strip():return None- normalized_bbox = _normalize_bbox_to_thousandths(bbox, page_size)+ normalized_bbox = _normalize_bbox_to_unit(bbox, page_size)output_block = {"type": normalized_type,"bbox": normalized_bbox,@@ -929,7 +929,7 @@ def _normalize_output_block(continueregion = _clip_bbox(raw_region, page_size)if region is not None:- inline_math_regions.append(_normalize_bbox_to_thousandths(region, page_size))+ inline_math_regions.append(_normalize_bbox_to_unit(region, page_size))if inline_math_regions:output_block["_inline_math_regions"] = inline_math_regionsif normalized_type in _LINE_METADATA_OUTPUT_TYPES:@@ -963,5 +963,5 @@ def _normalize_output_line_items()if page_bbox is None:return []- line_items.append({"bbox": _normalize_bbox_to_thousandths(page_bbox, page_size)})+ line_items.append({"bbox": _normalize_bbox_to_unit(page_bbox, page_size)})return line_itemsdiff --git a/mineru/utils/image.py b/mineru/utils/image.pyindex 2b8a9f30..ea260788 100644--- a/mineru/utils/image.py+++ b/mineru/utils/image.py@@ -34,12 +34,16 @@ def calculate_contrast(img: np.ndarray, img_mode: str) -> float:return round(float(contrast), 2)-def crop_pil_image(bbox: BBox, image: Image.Image, scale: float = 2.0) -> Image.Image:- """按缩放后的 bbox 裁剪 Pillow 图像。"""- scaled_bbox = normalize_to_int_bbox([float(value) * scale for value in bbox])+def crop_pil_image(bbox: BBox, image: Image.Image) -> Image.Image:+ """按 0-1 归一化 bbox 裁剪 Pillow 图像。"""+ width, height = image.size+ scaled_bbox = normalize_to_int_bbox(+ [bbox[0] * width, bbox[1] * height, bbox[2] * width, bbox[3] * height],+ image_size=(height, width),+ )if scaled_bbox is None:return image.crop((0, 0, 0, 0))- return image.crop(tuple(scaled_bbox))+ return image.crop(scaled_bbox)__all__ = ["calculate_contrast", "crop_pil_image"]