Convert Markdown to Image in Python

If you need to preview a Markdown file, you can convert it to image formats. Using Aspose.HTML for Python via .NET, you can easily convert Markdown to JPG, PNG, BMP, GIF or TIFF files with just a few lines of code!

This article provides information on how to convert Markdown to Image formats using the Converter class. You will learn about the supported conversion scenarios and consider Python code examples to illustrate them. Also, you can try an Online Markdown Converter to test the Aspose.HTML API functionality and convert Markdown on the fly.

Note: All of the convert_markdown() methods of the Converter class allow for the basic Markdown to HTML conversion. Conversions from Markdown to other formats go through the Markdown to HTML conversion stage.

Online Markdown Converter

You can convert Markdown to other formats with Aspose.HTML in real time. Please load Markdown from a local file system or URL, select the output format and run the example. The save options are set by default. You will immediately receive the conversion result as a separate file.

                
            

If you want to convert Markdown to Image formats programmatically, please see the following Python code examples.

Convert Markdown to PNG

Conversions from Markdown to other formats go through the Markdown to HTML conversion stage. To convert Markdown to PNG, you should follow a few steps:

  1. Load a source Markdown document. In the example, we create a Markdown file from code.
  2. Prepare a path for converted file saving.
  3. Convert Markdown to HTML. Use the convert_markdown() method to save Markdown as an HTML document.
  4. Create an instance of the ImageSaveOptions class. By default, the format property is PNG. Here, you can set the required save options, such as page setup, resolution, etc.
  5. Use the convert_html() method to render the intermediary HTML document to PNG image. In the following example, you need to pass the HTMLDocument, ImageSaveOptions, and output file path save_path to the convert_html() method for converting HTML to PNG.

If your case is to create a Markdown document from a user string directly in your code and convert it to a PNG image, the following example could help you:

 1# Convert Markdown to PNG using Python
 2
 3import os
 4import aspose.html.converters as conv
 5import aspose.html.saving as sav
 6
 7# Setup output directory and paths
 8output_dir = "output/"
 9os.makedirs(output_dir, exist_ok=True)
10source_path = os.path.join(output_dir, "document.md")
11
12# Create a simple Markdown example file
13code = "### Hello, World!\nConvert Markdown to PNG!"
14with open(source_path, "w") as file:
15    file.write(code)
16
17# Convert Markdown file to an intermediate HTMLDocument
18document = conv.Converter.convert_markdown(source_path)
19
20# Create ImageSaveOptions with PNG format
21options = sav.ImageSaveOptions()
22
23# Prepare output file path
24save_path = os.path.join(output_dir, "markdown-to-image.png")
25
26# Convert HTMLDocument to PNG image
27conv.Converter.convert_html(document, options, save_path)

Aspose.HTML offers a free online MD to PNG Converter that converts Markdown to PNG image with high quality, easy and fast. Just upload, convert your files and get the result in a few seconds!

Convert Markdown to JPG using ImageSaveOptions

The process of converting Markdown to Image can be flexibly customized. The ImageSaveOptions class offers extensive customization for converting HTML content into image formats. Here is a detailed description of each property of ImageSaveOptions:

If you want to convert an existing Markdown document from a local file system using custom save options, the following example could help you:

 1# Convert Markdown to JPG using Python with custom settings
 2
 3import os
 4import aspose.html.converters as conv
 5import aspose.html.saving as sav
 6import aspose.html.drawing as dr
 7import aspose.html.rendering.image as rim
 8
 9# Setup directories and define paths
10output_dir = "output/"
11input_dir = "data/"
12if not os.path.exists(output_dir):
13    os.makedirs(output_dir)
14document_path = os.path.join(input_dir, "document.md")
15save_path = os.path.join(output_dir, "md-to-jpg-with-save-options.jpg")
16
17# Convert Markdown to HTML
18document = conv.Converter.convert_markdown(document_path)
19
20# Create an instance of ImageSaveOptions
21options = sav.ImageSaveOptions(rim.ImageFormat.JPEG)
22options.horizontal_resolution = dr.Resolution.from_dots_per_inch(200.0)
23options.vertical_resolution = dr.Resolution.from_dots_per_inch(200.0)
24options.css.media_type.PRINT
25
26# Convert HTML to JPG
27conv.Converter.convert_html(document, options, save_path)

In this code, the ImageSaveOptions class from Aspose.HTML for Python via .NET is used to customize the conversion of Markdown to JPEG.

How to Convert Markdown to Images

Aspose.HTML for Python via .NET supports converting Markdown to PNG, JPG, JPEG, BMP, TIFF, and GIF images. You can use the above Python code for this; to set the output image format, you only need to specify the required extension (format) in the output file name and set the format property for the save options object.

For example, to convert Markdown to BMP, you need:

Download the Aspose.HTML for Python via .NET library allows you to successfully, quickly, and easily convert your HTML, MHTML, EPUB, SVG, and Markdown documents to the most popular formats.

You can check the quality of Markdown to JPG conversion with our online MD to JPG Converter. Upload, convert your files and get results in a few seconds. Try our forceful Markdown to JPG Converter for free now!

Text “MD to JPG Converter”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.