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: consolidate image loading error handling for improved robustness
myhlolia7f92f8dca2e
diff --git a/mineru/backend/utils/office_image.py b/mineru/backend/utils/office_image.pyindex 3a1217f9..5a3ecd0f 100644--- a/mineru/backend/utils/office_image.py+++ b/mineru/backend/utils/office_image.py@@ -20,6 +20,7 @@ VECTOR_IMAGE_CONTENT_TYPES = frozenset("application/x-msmetafile",})+PIL_IMAGE_LOAD_ERRORS = (UnidentifiedImageError, OSError, SyntaxError)def is_vector_image(pil_image: Image.Image) -> bool:@@ -129,7 +130,7 @@ def serialize_vector_image_with_placeholder(try:pil_image.load()return image_to_b64str(pil_image, image_format="PNG")- except OSError as e:+ except PIL_IMAGE_LOAD_ERRORS as e:logger.warning(f"Failed to render {image_format} image: {e}, size: {pil_image.size}. Using placeholder instead.")@@ -179,12 +180,28 @@ def serialize_office_image(content_type: str | None = None,) -> str | None:if is_vector_image_part(part_name, content_type):- return serialize_vector_part_with_placeholder(part_name, content_type)+ if not is_windows_environment():+ return serialize_vector_part_with_placeholder(part_name, content_type)++ try:+ pil_image = Image.open(BytesIO(image_data))+ except PIL_IMAGE_LOAD_ERRORS as e:+ logger.warning(+ f"Warning: vector image cannot be opened by Pillow: {e}, "+ f"part_name={part_name}, content_type={content_type}. "+ "Using placeholder instead."+ )+ return serialize_vector_part_with_placeholder(part_name, content_type)++ return serialize_vector_image_with_placeholder(+ pil_image,+ image_format_override=_vector_image_format_label(part_name, content_type),+ )try:pil_image = Image.open(BytesIO(image_data))pil_image.load()- except (UnidentifiedImageError, OSError) as e:+ except PIL_IMAGE_LOAD_ERRORS as e:logger.warning(f"Warning: image cannot be loaded by Pillow: {e}, "f"part_name={part_name}, content_type={content_type}"diff --git a/mineru/model/pptx/pptx_converter.py b/mineru/model/pptx/pptx_converter.pyindex f95a70ec..7d98066b 100644--- a/mineru/model/pptx/pptx_converter.py+++ b/mineru/model/pptx/pptx_converter.py@@ -10,10 +10,11 @@ from pptx import Presentation, presentationfrom pptx.enum.shapes import MSO_SHAPE_TYPE, PP_PLACEHOLDERfrom pptx.oxml.text import CT_TextLineBreakfrom loguru import logger-from PIL import Image, ImageFile, UnidentifiedImageError+from PIL import Imagefrom mineru.utils.enum_class import BlockTypefrom mineru.backend.utils.office_image import (+ PIL_IMAGE_LOAD_ERRORS,is_vector_image,serialize_vector_image_with_placeholder,)@@ -103,6 +104,11 @@ class PptxConverter:self,file_stream: BinaryIO,):+ # 兼容上游预读同一个 stream 后再交给 PPTX 转换器的场景。+ try:+ file_stream.seek(0)+ except (AttributeError, OSError, ValueError):+ passfile_bytes = file_stream.read()try:self._convert_package_bytes(file_bytes)@@ -722,20 +728,9 @@ class PptxConverter:"""尽量序列化图片;损坏图片失败时降级跳过,避免中断整份 PPTX。"""try:return cls._serialize_picture_image(image_bytes)- except (UnidentifiedImageError, OSError, SyntaxError) as exc:- logger.warning(- f"Warning: image cannot be loaded by Pillow, retrying with truncated-image mode: {exc}"- )-- previous_truncated_setting = ImageFile.LOAD_TRUNCATED_IMAGES- ImageFile.LOAD_TRUNCATED_IMAGES = True- try:- return cls._serialize_picture_image(image_bytes)- except (UnidentifiedImageError, OSError, SyntaxError) as exc:+ except PIL_IMAGE_LOAD_ERRORS as exc:logger.warning(f"Warning: image cannot be loaded by Pillow: {exc}")return None- finally:- ImageFile.LOAD_TRUNCATED_IMAGES = previous_truncated_setting@staticmethoddef _serialize_picture_image(image_bytes: bytes) -> str: