Extract embedded PDF images as separate files with relative Markdown links#2055
Open
haydaramru wants to merge 2 commits into
Open
Extract embedded PDF images as separate files with relative Markdown links#2055haydaramru wants to merge 2 commits into
haydaramru wants to merge 2 commits into
Conversation
Author
|
@microsoft-github-policy-service agree |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds an opt-in PDF image extraction workflow for MarkItDown. When enabled, embedded images from PDFs are saved as separate files under an output directory, and the generated Markdown references them using relative links.
This is intended for RAG, document archival, technical documentation, datasheets, schematics, research papers, and other workflows where inline base64 images make Markdown too large or impractical for indexing.
Example:
Output:
Markdown:
What changed
CLI
Adds two new CLI options:
--output-diris required when--extract-imagesis used. Existing stdout behavior is preserved: if-ois not provided, Markdown is still printed to stdout, while images are written under--output-dir/images/.Python API
Adds constructor-level support:
Also supports per-call overrides:
If image extraction is enabled without an output directory, MarkItDown raises:
PDF conversion
The PDF converter now has an opt-in image extraction path. Default PDF conversion remains unchanged unless
extract_images=Trueis explicitly set.The implementation:
pdfplumberpage image objects.output_dir/images/.page1-image1.pngpage1-image2.pngpage3-image1.jpgpage.close().Image writing behavior
The converter first tries to save original embedded image stream bytes when they are already recognizable image formats, including:
If direct stream extraction is not possible, it falls back to rendering/cropping the image region from the PDF page using
pdfplumber.The current fallback render setting is:
This improves output sharpness for diagrams and technical images, but produces larger files and may be slower than lower DPI values such as
150or300.Why not inline base64?
Before this feature, PDF/image-heavy Markdown workflows often had two poor options:
Both are problematic for RAG and indexing. Extracted image files with relative links keep Markdown lightweight while preserving visual context.
Testing
Added focused coverage for image extraction, relative Markdown links,
output_dirvalidation, CLI help/validation, and existing PDF page cleanup behavior. Fullhatch testandpre-commitchecks was also run andPassed.Manual verification
Example command:
Expected files:
Example Markdown output:
Files changed
Core implementation:
packages/markitdown/src/markitdown/__main__.pypackages/markitdown/src/markitdown/_markitdown.pypackages/markitdown/src/markitdown/converters/_pdf_converter.pyTests:
packages/markitdown/tests/test_cli_misc.pypackages/markitdown/tests/test_pdf_images.pyFuture improvements
Possible follow-up work:
--image-dpi 300.--image-dir assets/images.Related issues
This feature addresses the same workflow need described in Issue #2049 and complements prior work around base64 data URI handling and image descriptions for RAG workflows.