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
feat: enhance PDF generation by preserving original image raster content and optimizing image handling
myhloli7a365d92c96a
diff --git a/mineru/utils/pdf_image_tools.py b/mineru/utils/pdf_image_tools.pyindex 545e3a9c..468bb6a6 100644--- a/mineru/utils/pdf_image_tools.py+++ b/mineru/utils/pdf_image_tools.py@@ -8,6 +8,8 @@ import numpy as npimport pypdfium2 as pdfiumfrom loguru import loggerfrom PIL import Image, ImageOps+from reportlab.lib.utils import ImageReader+from reportlab.pdfgen import canvasfrom mineru.data.data_reader_writer import FileBasedDataWriterfrom mineru.utils.check_sys_env import is_windows_environment@@ -331,29 +333,35 @@ def get_crop_np_img(bbox: tuple, input_img, scale=2):def images_bytes_to_pdf_bytes(image_bytes):- # 内存缓冲区pdf_buffer = BytesIO()- # 载入并转换所有图像为 RGB 模式- image = Image.open(BytesIO(image_bytes))- # 根据 EXIF 信息自动转正(处理手机拍摄的带 Orientation 标记的图片)- image = ImageOps.exif_transpose(image) or image- # 只在必要时转换- if image.mode != "RGB":- image = image.convert("RGB")-- # 第一张图保存为 PDF,其余追加- # Keep image inputs at the same raster size when CLI later renders the- # wrapper PDF at the default DPI; PIL defaults to 72 dpi for PDF output, which- # would upscale the image and noticeably hurt seal OCR detection quality.- image.save(- pdf_buffer,- format="PDF",- resolution=float(DEFAULT_PDF_IMAGE_DPI),- # save_all=True+ with Image.open(BytesIO(image_bytes)) as source_image:+ image = ImageOps.exif_transpose(source_image)+ if image.mode != "RGB":+ image = image.convert("RGB")+ else:+ image = image.copy()++ # Preserve the original raster content when the CLI later renders the+ # wrapper PDF back at DEFAULT_PDF_IMAGE_DPI. PIL's PDF writer introduces+ # enough loss for layout detection to miss text blocks on some image inputs.+ page_width = image.width * 72.0 / DEFAULT_PDF_IMAGE_DPI+ page_height = image.height * 72.0 / DEFAULT_PDF_IMAGE_DPI++ pdf_canvas = canvas.Canvas(pdf_buffer, pagesize=(page_width, page_height))+ pdf_canvas.drawImage(+ ImageReader(image),+ 0,+ 0,+ width=page_width,+ height=page_height,+ preserveAspectRatio=False,+ mask="auto",)+ pdf_canvas.showPage()+ pdf_canvas.save()- # 获取 PDF bytes 并重置指针(可选)pdf_bytes = pdf_buffer.getvalue()pdf_buffer.close()+ image.close()return pdf_bytes