How to convert HTML to image using wkhtmltoimage and Python
Table of contents
Convert HTML to JPG, PNG, and other image formats with wkhtmltoimage and Python. Covers installation, scripting, customization, alternatives like Puppeteer and Playwright, and troubleshooting.
Convert HTML to JPG, PNG, and other image formats using wkhtmltoimage(opens in a new tab) and Python. This common web development task enables creating screenshots, social media graphics, email signatures, reports, and documentation. wkhtmltoimage is a command-line utility that converts HTML to PNG, JPEG (JPG), BMP, and TIFF formats using the WebKit rendering engine. The resulting images look similar to what you see in a web browser, and you can customize output with options for image size, quality, and cropping. If you also need to convert HTML to PDF, the related wkhtmltopdf tool covers that workflow.
Development considerations
wkhtmltoimage is a command-line tool without a graphical user interface (GUI). It’s an open source tool — created in 2008 by Jakob Truelsen — and it isn’t being consistently maintained(opens in a new tab) at this time.
If you’re considering a commercial solution, Nutrient offers an HTML-to-image API in Python. Our hosted solution gives you 50 free credits, and we offer additional packages for a per-document fee. Our solutions are regularly maintained, with releases occurring multiple times throughout the year. We also offer one-on-one support to handle any issues you encounter. You can also automate conversions through our Zapier HTML-to-image integration.
Installing wkhtmltoimage
Download the appropriate binary for your operating system from the project’s website(opens in a new tab). The installation process varies by platform.
Once the download is complete, run the installer and follow the prompts to complete the installation.
- On Windows: The wkhtmltoimage executable will be located in the installation directory — by default, it’s
C:\Program Files\wkhtmltoimage\bin. - On Mac: The wkhtmltoimage executable will be located in the installation directory — by default, it’s
/usr/local/bin.
You can check the path of the wkhtmltoimage executable on your system by running the command where wkhtmltoimage on Windows and which wkhtmltoimage on Mac in the command line.
Integrating wkhtmltoimage with Python
- Create a new directory with a file called
main.py. This file will contain the conversion code. - Use wkhtmltoimage in your Python script by calling the
subprocess(opens in a new tab) module to run the command and capture the output. The subprocess module enables you to start new processes from within your Python script:
import subprocess
# Use subprocess to call the wkhtmltoimage command.subprocess.run(['wkhtmltoimage', 'input.html', 'output.png'])In the example above, the input file is input.html, and the output file is output.png. The output format is determined by the file extension — use .jpg for JPEG, .png for PNG, or .bmp for bitmap. The subprocess.run(opens in a new tab) function returns a CompletedProcess object. Check result.returncode to confirm the conversion succeeded (0 means success), or pass check=True to raise a CalledProcessError automatically on non-zero exit status.
Customizing the output
Customize the output by passing additional options to the wkhtmltoimage command. You can specify width, height, quality, and cropping:
subprocess.run(["wkhtmltoimage", "--width", "800", "--height", "600", "input.html", "output.png"])This sets the image to 800 pixels wide and 600 pixels tall.
The -q option runs the command in quiet mode, suppressing console output:
subprocess.run(["wkhtmltoimage", "-q", "input.html", "output.png"])- The
--crop-wand--crop-hoptions can be used to crop the image to a specific width and height. For example,--crop-w 800--crop-h 600will crop the image to 800 pixels wide and 600 pixels high:
import subprocess
# Read the input file in read mode.with open("input.html", "r") as f: html_data = f.read()
options = ['--quality', '100', '--crop-w', '800', '--crop-h', '600']
subprocess.run(['wkhtmltoimage'] + options + ['-', "output.png"], input=html_data.encode())# orsubprocess.run(['wkhtmltoimage'] + options + ['-', "output.png"], input=bytes(html_data, 'utf-8'))In the example above, the options set the quality and crop size of the output image. The input parameter of subprocess.run() expects a byte-like object, so convert the string using either encode() or bytes().
See the full list of options(opens in a new tab) for additional flags.
Converting HTML to JPG format
To produce a JPG (JPEG) file instead of PNG, change the output file extension and use the --quality flag to control compression. JPG is a good choice when you need smaller file sizes and don’t require transparency:
subprocess.run(["wkhtmltoimage", "--format", "jpg", "--quality", "85", "input.html", "output.jpg"])The --quality flag accepts values from 0 to 100, where higher values produce better quality at the cost of larger file sizes. A value between 80 and 90 typically offers a good balance. For converting other document types to images, see our guide on converting PDF to JPG with Python.
Alternatives to wkhtmltoimage
wkhtmltoimage isn’t the only tool for HTML-to-image conversion. Here’s how common alternatives compare:
| Feature | wkhtmltoimage | Puppeteer | Playwright | Nutrient API |
|---|---|---|---|---|
| Engine | WebKit (older) | Chromium | Chromium/Firefox/WebKit | Cloud-based |
| JavaScript support | Limited | Full | Full | Full |
| Maintenance | Unmaintained | Active | Active | Commercially supported |
| Setup complexity | Simple | Moderate | Moderate | Minimal (API calls) |
| Output formats | PNG, JPG, BMP, TIFF | PNG, JPG, WebP | PNG, JPG | PNG, JPG, WebP, TIFF |
| Modern CSS support | Partial | Full | Full | Full |
| Headless mode | Yes | Yes | Yes | N/A (cloud) |
| Best for | Simple static pages | Dynamic JS-heavy pages | Cross-browser testing | Production workflows |
For projects that need modern CSS and JavaScript rendering, consider migrating from Puppeteer to Playwright. For production-scale conversion, the Nutrient HTML-to-image API eliminates the need to manage browser dependencies.
Common issues and troubleshooting
Common problems during HTML-to-JPG and HTML-to-image conversion:
- Blank or white output — The HTML page may rely on JavaScript to render content. wkhtmltoimage has limited JavaScript support, so add
--javascript-delay 2000to wait for scripts to execute, or consider a Chromium-based tool like Puppeteer. - Missing fonts or broken layout — Install the required fonts on the server. On Linux, install
fontconfigand common font packages. Use--encoding utf-8if special characters appear garbled. - “Cannot connect to X server” on Linux — wkhtmltoimage requires a display server. Install
xvfband run withxvfb-run wkhtmltoimage input.html output.jpg. - Low-quality JPG output — Set
--quality 90or higher. The default quality can produce visible compression artifacts, especially for text-heavy pages. - CSS not loading from external stylesheets — Use
--enable-local-file-accesswhen converting local HTML files, or inline your CSS to avoid path resolution issues. - Large file sizes — For JPG, reduce
--qualityto 70–80. For PNG, reduce--widthor use a tool that supports WebP output for better compression.
For recurring conversion tasks or automated reporting, refer to our guides on generating images from text and Zapier HTML-to-image automation.
Conclusion
wkhtmltoimage handles basic HTML-to-image conversion well for static pages. For related document conversion workflows, check out how to convert HTML to PDF using wkhtmltopdf and Python or explore the top HTML-to-PDF conversion tools.
For a commercially supported alternative, Nutrient offers an HTML-to-image API that can be integrated into your workflow or application. You can also use it alongside our HTML-to-PDF conversion tools in JavaScript and other languages. Create an account to unlock 50 free credits per month.
FAQ
wkhtmltoimage is a command-line tool that converts HTML to various image formats, such as PNG, JPEG (JPG), BMP, and TIFF, using the WebKit rendering engine. It renders HTML the same way a browser would, producing pixel-accurate screenshots of web content.
Use the subprocess module to call wkhtmltoimage with the --format jpg flag and a .jpg output extension. Set --quality between 80 and 90 for a good balance of file size and image quality. For example: subprocess.run(["wkhtmltoimage", "--format", "jpg", "--quality", "85", "input.html", "output.jpg"]).
PNG produces lossless images with transparency support, making it ideal for screenshots with sharp text and graphics. JPG uses lossy compression, resulting in smaller file sizes but without transparency. Choose JPG when file size matters (social media, email) and PNG when you need pixel-perfect accuracy or transparent backgrounds.
Download the appropriate binary for your operating system from the official website(opens in a new tab) and follow the installation instructions. The install location depends on your OS and installation method, so verify the binary path by running which wkhtmltoimage (Mac/Linux) or where wkhtmltoimage (Windows). Common locations include /usr/local/bin or /opt/homebrew/bin on Mac (depending on installation method), and C:\Program Files\wkhtmltoimage\bin on Windows.
Import the subprocess module and call subprocess.run(['wkhtmltoimage', 'input.html', 'output.png']). The output format is determined by the file extension — use .jpg for JPEG, .png for PNG, or .bmp for bitmap. Pass additional flags like --width, --height, and --quality to customize the output.
This usually happens because the HTML relies on JavaScript to render content, and wkhtmltoimage has limited JavaScript support. Add --javascript-delay 2000 to wait for scripts to execute. If the page is heavily JavaScript-dependent, consider using Puppeteer or Playwright instead, as they use a full Chromium browser engine.
Yes. Nutrient API offers an HTML-to-image API in Python with 50 free credits per month. It’s actively maintained, with regular updates, one-on-one support, and integration with 30+ other document processing tools.
Yes. Nutrient offers an HTML-to-image API with 50 free credits per month, providing a commercially supported solution with full modern CSS and JavaScript rendering. For batch automation, you can also use the Zapier HTML-to-image integration to convert HTML files without writing code.
Yes. Install xvfb (X Virtual Framebuffer) and prefix your command with xvfb-run. For example: xvfb-run wkhtmltoimage input.html output.jpg. This creates a virtual display so wkhtmltoimage can render the page without a physical monitor, which is essential for server environments and CI/CD pipelines.
Related
HTML-to-image conversion: Convert HTML to image with wkhtmltoimage and Laravel, Zapier HTML-to-image automation, creating images from text on the web
HTML-to-PDF conversion: HTML to PDF with wkhtmltopdf and Python, top HTML-to-PDF conversion tools, HTML to PDF in JavaScript, HTML to PDF using html2pdf.js, generate PDF reports from HTML in Python
Image and document conversion: Convert PDF to JPG using Python, convert DOCX to WebP in Python
Browser automation: Migrating from Puppeteer to Playwright