Markdown to Image in Python – Markdown to JPG, PNG, BMP, GIF, TIFF

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:

 1import os
 2from aspose.html import *
 3from aspose.html.saving import *
 4from aspose.html.converters import Converter
 5
 6# Prepare a path to a source Markdown file
 7output_dir = "output/"
 8source_path = os.path.join(output_dir, "document.md")
 9
10# Prepare a simple Markdown example
11code = "### Hello, World!\nConvert Markdown to PNG!"
12
13# Create a Markdown file
14with open(source_path, "w") as file:
15    file.write(code)
16
17# Convert Markdown to HTML document
18document = Converter.convert_markdown(source_path)
19
20# Create an instance of ImageSaveOptions
21options = ImageSaveOptions()
22
23# Prepare a path to save the converted file
24save_path = os.path.join(output_dir, "markdown-to-image.png")
25
26# Convert HTML to PNG
27Converter.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:

 1import os
 2from aspose.html import *
 3from aspose.html.converters import *
 4from aspose.html.saving import *
 5from aspose.html.drawing import *
 6
 7# Setup directories and define paths
 8output_dir = "output/"
 9input_dir = "data/"
10if not os.path.exists(output_dir):
11    os.makedirs(output_dir)
12document_path = os.path.join(input_dir, "document.md")
13save_path = os.path.join(output_dir, "markdown-to-image.jpeg")
14
15# Convert Markdown to HTML
16document = Converter.convert_markdown(document_path)
17
18# Create an instance of ImageSaveOptions
19options = ImageSaveOptions()
20options.format.JPEG
21options.horizontal_resolution = Resolution.from_dots_per_inch(200.0)
22options.vertical_resolution = Resolution.from_dots_per_inch(200.0)
23options.css.media_type.PRINT
24
25# Convert HTML to JPG
26Converter.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 our 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.