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:
- Load a source Markdown document. In the example, we create a Markdown file from code.
- Prepare a path for converted file saving.
- Convert Markdown to HTML. Use the convert_markdown() method to save Markdown as an HTML document.
- 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. - 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 pathsave_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
:
- page_setup – You can configure the page layout settings for the output image. This includes specifying the page size and margins (top, bottom, left, right) to control the placement and display of HTML content within the image.
- horizontal_resolution – This property sets or gets the horizontal resolution (in pixels per inch) for both output and internal images used during processing. Higher resolution usually results in a sharper image but can also increase file size. By default, it is 300 dpi.
- vertical_resolution – This property sets or gets the vertical resolution for internal images in pixels per inch. By default, it is 300 dpi.
- background_color – This property allows you to set the background color for the rendered output. If not set, the default background is transparent.
- css – This property, represented by
CssOptions
, allows configuring how CSS properties are processed during the HTML to image conversion. - format – This property determines the format of the output image. The supported formats include common image formats like PNG, JPEG, BMP, GIF, and TIFF. The default format is PNG, but you can specify others based on your requirements.
- smoothing_mode – This property controls the quality of graphics rendering during conversion. It affects how images are rendered, which is especially useful for anti-aliasing and achieving smooth and visually appealing output. Options typically include settings for high-quality rendering, which can be critical for professional and presentation-grade images.
- compression – The compression option allows you to set the compression method for TIFF output. Supported options: LZW, CCITT3, CCITT4, RLE, and NONE. Compression helps reduce file size while maintaining image quality, which is especially important for TIFF files used for high-quality archiving and printing of images.
- text – This property provides configurations for text rendering during HTML to image conversion.
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.
- The
format
is set to JPEG to define the output image format. - The
horizontal_resolution
andvertical_resolution
properties are both set to 200 dots per inch (dpi) to ensure high-quality image rendering. - The
css.media_type
is set to PRINT to specify that the CSS media type for print should be used during conversion. These configurations ensure that the resulting JPEG image is of high resolution and formatted according to print media standards.
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:
- to set the format property:
options.format.BMP
- and set the extension
.bmp
in the output image file name:save_path = os.path.join(output_dir, "markdown-to-image.bmp")
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!