A scanned report may contain text that you want to edit or add to your documentation. You can use OCR to recognize the text and save it as Markdown, making it easier to review and reuse in a Python application.

In this article, you’ll learn how to convert a PNG or JPG image to Markdown in Python using Aspose.OCR for Python via .NET. You’ll also see how to review the generated content and check the document’s tables and illustrations.

Python OCR Library for Image-to-Markdown Conversion

Aspose.OCR for Python via .NET lets you recognize text in images and save OCR results as Markdown. Its document layout detection mode can identify elements such as text blocks, tables, and images.

Install version 26.10.0 of the package with pip:

python -m pip install aspose-ocr-python-net

You’ll need a supported Python installation, a readable PNG or JPG image, and permission to write the output file. To follow the example, place an image named report.png in the directory where you’ll run the script. A separate .NET runtime installation is not required; see the system requirements for supported platforms.

The trial version limits the amount of recognized text. To remove this restriction during evaluation, you can request a temporary license and apply it before recognition. See the licensing documentation for details.

Convert an Image to Markdown in Python

Follow these steps to recognize an image and save the result as Markdown:

  1. Create an instance of the AsposeOcr class.
  2. Create an OcrInput object for a single image and add the image file.
  3. Create a RecognitionSettings object and set detect_areas_mode to DOCUMENT_LAYOUT.
  4. Call the recognize() method to process the image.
  5. Save the results using save() and SaveFormat.MD.

For this example, use a report image containing a heading, text, a table, and an illustration. Save the following code in a file named image_to_markdown.py:

import aspose.ocr as ocr

# Create the OCR engine.
api = ocr.AsposeOcr()

# Add the image to the input.
ocr_input = ocr.OcrInput(ocr.InputType.SINGLE_IMAGE)
ocr_input.add("report.png")

# Enable document layout detection.
settings = ocr.RecognitionSettings()
settings.detect_areas_mode = ocr.DetectAreasMode.DOCUMENT_LAYOUT

# Recognize the image.
results = api.recognize(ocr_input, settings)

# Save the recognition result as Markdown.
results.save("report.md", ocr.SaveFormat.MD)

Run the script from the directory containing report.png:

python image_to_markdown.py

The script reads report.png and saves the recognition result as report.md. Both paths are relative to the current working directory. To convert a JPG image, change the input path to your .jpg file; the recognition and export steps remain the same.

The DOCUMENT_LAYOUT mode is intended for documents whose structure matters in the output. Detection depends on the source image, so review the generated Markdown against the original report.

Review the Generated Markdown

Open report.md in a text editor or Markdown viewer. Check the recognized text, reading order, and any table content against the source image. You can then edit the Markdown before adding it to your documentation.

For example, a table in Markdown may look like the following. This is illustrative syntax, not actual OCR output:

| Product | Units Sold | Revenue (USD) |
| --- | --- | --- |
| Product A | 120 | $18,000 |

If the generated Markdown contains references to extracted images, check that the referenced files exist and appear in your Markdown viewer. Keep their paths intact when moving the Markdown file.

Markdown describes content and structure; it does not necessarily reproduce the exact appearance of the original report.

Tips for Better OCR Results

  • Use a clear source image. Text and table borders should be readable in the original image.
  • Choose the appropriate recognition language. Set RecognitionSettings.language when needed. See the language documentation for available options.
  • Check important details. Compare names, numbers, table cells, and reading order with the source.
  • Verify image references. If the output references extracted images, open the Markdown in a viewer and check that they appear.
  • Test representative documents. Complex tables and layouts may need manual corrections, so review examples before using the output in an automated workflow.

Frequently Asked Questions

Can I convert both PNG and JPG images to Markdown in Python?

Yes. Add the PNG or JPG file to OcrInput, recognize it, and save the result with SaveFormat.MD.

Can Python OCR detect tables in an image?

Yes. The DOCUMENT_LAYOUT mode is designed to detect document elements, including tables. Review the generated Markdown to check how the table and its cells are represented.

How do I handle images referenced by the Markdown file?

If the output contains image links, locate the referenced files and keep their paths intact when moving the Markdown. Open the file in a Markdown viewer to confirm that the images display.

Does the Markdown file preserve the original page layout exactly?

No. Markdown represents content and structure differently from a page image. Review the generated file and adjust its structure where needed.

Conclusion

Use this workflow as a starting point for converting your own report images to Markdown in Python. Before adding the results to your documentation, compare the recognized text, tables, and any image references with the original.

For more information, see the recognition documentation. If you need help, visit the Aspose.OCR support forum.

See Also