From f6a17fbc564ee8e75cdbe376f22171f78fc86159 Mon Sep 17 00:00:00 2001 From: amuwall Date: Tue, 18 Mar 2025 10:09:02 +0800 Subject: [PATCH] community: fix import exception too constrictive (#30218) Fix this issue #30097 --- .../document_loaders/parsers/images.py | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/libs/community/langchain_community/document_loaders/parsers/images.py b/libs/community/langchain_community/document_loaders/parsers/images.py index b053b94d491..eb3da4d2a11 100644 --- a/libs/community/langchain_community/document_loaders/parsers/images.py +++ b/libs/community/langchain_community/document_loaders/parsers/images.py @@ -45,24 +45,24 @@ class BaseImageBlobParser(BaseBlobParser): """ try: from PIL import Image as Img - - with blob.as_bytes_io() as buf: - if blob.mimetype == "application/x-npy": - img = Img.fromarray(numpy.load(buf)) - else: - img = Img.open(buf) - content = self._analyze_image(img) - logger.debug("Image text: %s", content.replace("\n", "\\n")) - yield Document( - page_content=content, - metadata={**blob.metadata, **{"source": blob.source}}, - ) except ImportError: raise ImportError( "`Pillow` package not found, please install it with " "`pip install Pillow`" ) + with blob.as_bytes_io() as buf: + if blob.mimetype == "application/x-npy": + img = Img.fromarray(numpy.load(buf)) + else: + img = Img.open(buf) + content = self._analyze_image(img) + logger.debug("Image text: %s", content.replace("\n", "\\n")) + yield Document( + page_content=content, + metadata={**blob.metadata, **{"source": blob.source}}, + ) + class RapidOCRBlobParser(BaseImageBlobParser): """Parser for extracting text from images using the RapidOCR library.