From 09c4c1f86757ebb1c650a418768c5c5577a0ba74 Mon Sep 17 00:00:00 2001 From: Philippe Prados Date: Wed, 26 Mar 2025 15:01:16 +0100 Subject: [PATCH] Fix images parser --- .../document_loaders/parsers/images.py | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/libs/community/langchain_community/document_loaders/parsers/images.py b/libs/community/langchain_community/document_loaders/parsers/images.py index e8df541291f..3d977aae973 100644 --- a/libs/community/langchain_community/document_loaders/parsers/images.py +++ b/libs/community/langchain_community/document_loaders/parsers/images.py @@ -45,27 +45,28 @@ class BaseImageBlobParser(BaseBlobParser): """ try: from PIL import Image as Img - - with blob.as_bytes_io() as buf: - if blob.mimetype == "application/x-npy": - try: - img = Img.fromarray(numpy.load(buf)) - except EOFError: - return # Ignore too small images - 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": + array = numpy.load(buf) + if array.ndim == 3 and array.shape[2] == 1: # Grayscale image + img = Img.fromarray(numpy.squeeze(array, axis=2), mode="L") + else: + img = Img.fromarray(array) + 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.