Skip to content

Add support for preserving base64 encoded images - #1140

Merged
Adam Fourney (afourney) merged 9 commits into
microsoft:mainfrom
BetterAndBetterII:feat-optional_b64
Mar 21, 2025
Merged

Adam Fourney (afourney) merged 9 commits into
microsoft:mainfrom
BetterAndBetterII:feat-optional_b64

Conversation

@BetterAndBetterII

@BetterAndBetterII Yuzhong Zhang (BetterAndBetterII) commented Mar 18, 2025 •

Copy link
Copy Markdown
Contributor

Issue Description

Currently, the markitdown library automatically truncates base64-encoded image data when converting HTML, DOCX, and PPTX files. This behavior may not be ideal for certain use cases, especially when users want to preserve complete image data in the converted Markdown documents.

Related:
#51
#58
#162
#246
#1102
#1139

Solution

Add a new parameter keep_data_uris that allows users to control whether base64-encoded image data is preserved during the conversion process.

Changes Made

  1. Modified _CustomMarkdownify class (in _markdownify.py):

    • Added a new parameter keep_data_uris (default: False) to control whether to preserve base64 data URIs
    • Updated the convert_img method to respect this parameter when processing image sources
  2. Updated HtmlConverter class (in _html_converter.py):

    • Modified the convert method to accept and pass through the keep_data_uris parameter from kwargs
    • Ensured this parameter is passed to the _CustomMarkdownify constructor
  3. Updated DocxConverter class (in _docx_converter.py):

    • Modified the convert method to extract and pass the keep_data_uris parameter to the HTML converter
  4. Updated PptxConverter class (in _pptx_converter.py):

    • Added conditional logic to handle images differently based on the keep_data_uris parameter
    • When keep_data_uris=True, encode image blobs as base64 and include them directly in the Markdown
    • When keep_data_uris=False (default), continue using placeholder filenames
  5. Updated auxiliary converters:

    • BingSerpConverter: Added parameter forwarding for keep_data_uris
    • RssConverter: Added __init__ method to store kwargs and modified _parse_content to use the parameter
    • WikipediaConverter: Added parameter forwarding for keep_data_uris
  6. Verified YouTubeConverter:

    • Confirmed that despite importing _CustomMarkdownify, it does not actually use it for conversion

Benefits

  1. User Control: Users can now decide whether to include base64-encoded images in the output
  2. Consistency: All converters now handle the parameter in a unified way
  3. Backward Compatibility: The default behavior remains unchanged (truncating base64 data)

Usage Example

# To preserve base64 images when converting
result = converter.convert(
    file_stream=file,
    stream_info=stream_info,
    keep_data_uris=True
)

# To continue truncating base64 images (default behavior)
result = converter.convert(
    file_stream=file,
    stream_info=stream_info
)

This enhancement ensures a consistent approach to handling base64 image data across all converters in the markitdown library, providing flexibility while maintaining backward compatibility.

@afourney

Copy link
Copy Markdown
Member

Hey this looks great. Thanks for the contribution.

Would you be able to add some test cases to verify this behavior in the CI. Once we have reasonable test coverage, I think it's good to merge.

Comment on lines +82 to +84
_markdownify = _CustomMarkdownify(
keep_data_uris=kwargs.get("keep_data_uris", False)
)

@hoangquochung1110 Hoàng Quốc Hưng (hoangquochung1110) Mar 20, 2025 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_markdownify = _CustomMarkdownify(
keep_data_uris=kwargs.get("keep_data_uris", False)
)
_markdownify = _CustomMarkdownify(
**kwargs
)

By this way, we lay a "foundation" so that we can easily add new options to _CustomMarkdownify in the future without changing too much in upstream objects


def __init__(self, **options: Any):
options["heading_style"] = options.get("heading_style", markdownify.ATX)
self.keep_data_uris = options.pop("keep_data_uris", False)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

likewise, we can

Suggested change
self.keep_data_uris = options.pop("keep_data_uris", False)
options["keep_data_uris"] = options.get("keep_data_uris", False)

by this way, we have:

  • Default value in case the option is not supplied in upstream objects
  • Keep the implementation consistent

What do you think ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely! 🫡

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have modified it in the form of kwargs passing parameters. For the sake of uniformity, I used the kwargs passing parameter form in all places where _CustomMarkdownify is initialized, and also reserved adjustment space for adding more optional parameters later.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the command line parameter parser must declare parameters before parsing, kwargs cannot be used, so main.py still uses a fixed actual parameter passing method.

@BetterAndBetterII

Copy link
Copy Markdown
Contributor Author

Hey this looks great. Thanks for the contribution.

Would you be able to add some test cases to verify this behavior in the CI. Once we have reasonable test coverage, I think it's good to merge.

I have added tests for the cli and module calls. I mainly focus on office files such as word and powerpoint. Since Excel uses the pandas library to read, it does not support reading any images, so it is not possible, and I do not plan to add tests to retain images.

@afourney

Copy link
Copy Markdown
Member

Ok, there are some formatting issues, and I want to make one small change to how the tests are organized. Let me do that really quickly. Stay tuned.

@afourney

Copy link
Copy Markdown
Member

Yuzhong Zhang (@BetterAndBetterII) I fixed some formatting issues, and reorganized tests a little. If this looks good to you, let me know and I'll merge it.

@BetterAndBetterII

Copy link
Copy Markdown
Contributor Author

Yuzhong Zhang (@BetterAndBetterII) I fixed some formatting issues, and reorganized tests a little. If this looks good to you, let me know and I'll merge it.

great!

@harsh-devx

Copy link
Copy Markdown

quick question here, is the support for keep_data_uris is not extended for pdf converter?

@iocoker

Isaac Coker (iocoker) commented Jun 12, 2025 •

Copy link
Copy Markdown

quick question here, is the support for keep_data_uris is not extended for pdf converter?

I think the tool is predominantly built for .docx., .ppt files. Even basic styling is ignored in .pdfs and no images. You will only get plain texts from pdfs

@afourney

Copy link
Copy Markdown
Member

quick question here, is the support for keep_data_uris is not extended for pdf converter?

I think the tool is predominantly built for .docx., .ppt files. Even basic styling is ignored in .pdfs and no images. You will only get plain texts from pdfs

That is correct. Images in office files are their own entities and are easy to extract. PDFs, on the other hand, make things much hard (which is typically the case).

I have it on my to-do list to integrate or provide options to use much much better PDF parsers than what we currently ship (and indeed Azure Document Intelligence will already yield better results for PDFs). But until then, we basically are stuck with the text layer of the PDFs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants