A powerful Python utility that converts Markdown files to high-quality images with RTL/LTR language support and syntax highlighting.
- Convert any Markdown file to PNG image
- Support for RTL (Right-to-Left) and LTR (Left-to-Right) text directions
- Automatic language direction detection
- Light and dark themes
- Multiple font options including support for Arabic/Persian (Vazirmatn)
- Code syntax highlighting with Pygments
- Proper rendering of tables, links, lists, and blockquotes
- Customizable image width
- Line wrapping for long code blocks
-
Clone this repository:
git clone https://github.com/aminset/markdown-to-image.git cd markdown-to-image -
Install the required packages:
pip install markdown playwright pygments
-
Install the Playwright browser:
python -m playwright install chromium
Basic usage:
python md_to_image.py input.md -o output.pngWith options:
python md_to_image.py input.md -o output.png -w 800 -t dark -d rtl -f vazirmatn| Option | Description |
|---|---|
input_file |
Path to input Markdown file |
-o, --output |
Path to output image file (default: same as input with .png extension) |
-w, --width |
Width of the output image in pixels (default: 800) |
-t, --theme |
Theme for rendering: "light" or "dark" (default: dark) |
-d, --direction |
Text direction: "auto", "rtl", or "ltr" (default: auto) |
-f, --font |
Font family to use (see Font Options below) |
default: Arial, sans-serifvazirmatn: Vazirmatn - excellent for Persian/Arabicroboto: Robotoopensans: Open Sansdejavu: DejaVu Sansdejavusansmono: DejaVu Sans Mono - good for codesourcecodepro: Source Code Pro - great for codeplayfairdisplay: Playfair Display - elegant serif fontfiracode: Fira Code - programming font with ligaturesjetbrainsmononerd: JetBrains Mono - developer-focused font
Converting a Markdown file with auto-detection of text direction:
python md_to_image.py README.mdCreating a dark-themed image with custom width:
python md_to_image.py documentation.md -t dark -w 1000Converting a Persian/Arabic document:
python md_to_image.py persian_doc.md -d rtl -f jetbrainsmononerdConverting technical documentation with code:
python md_to_image.py code_doc.md -f sourcecodepro -w 900The tool converts Markdown to HTML, applies styling based on the selected theme and font, and then uses Playwright to render the HTML to a PNG image. It handles RTL/LTR text directions and automatically detects the primary language direction if not specified.
MIT License
Contributions are welcome! Please feel free to submit a Pull Request.