pymupdf / RAG

RAG (Retrieval-Augmented Generation) Chatbot Examples Using PyMuPDF
https://pymupdf.readthedocs.io/en/latest/pymupdf4llm
GNU Affero General Public License v3.0
518 stars 81 forks source link

Inconsistent image extraction from image-only PDFs #155

Open Cozokim opened 1 month ago

Cozokim commented 1 month ago

First of all, I'd like to mention that pymupdf4llm works very well for most use cases. I'm using the to_markdown function, which performs exactly as expected. Great work!

However, I've encountered an issue with PDFs that contain only images (though I am not entirely sure if this issue is directly related to the fact that the PDFs consist solely of images). In these cases, some images are missing from the output. Additionally, when an image contains text, instead of extracting the image as a whole, the library splits the image into multiple smaller images, each containing only one word.

Problems observed:

  1. Some images are not extracted even tho i've put the image_size_limit to a very low value
    1. In cases where an image contains text, instead of extracting it as a single image, multiple small images are created, each containing a single word.

I am unsure whether there is a way to merge these smaller images into one or if this behavior is caused by pymupdf4llm or the PDF format itself, which I understand can be quite complex to handle.

I am using write_images = True and have tried lowering the image_size_limit to a very small value, but this did not resolve the issue.

Thanks for your help, Cozokim

JorjMcKie commented 1 month ago

Please provide a reproducer.

Cozokim commented 1 month ago

Sure, on this PDF for example, some of the images extracted are going to be only isolated words, and the right text from the third page is also missing : first_three_pages_full_images.pdf

I'm using :

pdf = "data\pdf\first_three_pages_full_images.pdf"
doc = fitz.open(pdf)
md_text = to_markdown(doc, write_images=True,image_size_limit=0.001)
kalani9uggalle commented 2 weeks ago

i'm having the same issue when extracting pdfs only with images.