3 min read

Capturing screenshots with Chromium using Python

Sometimes you need to take screenshots of the web and Chromium provides an easy way to do that.

Home page screenshot of my blog
Author
Isaac Bythewood Isaac Bythewood
August 06, 2022

Chromium for a long time has provided a CLI for capturing web screenshots. I've found myself recently needing a to do a lot of this.

To start my script I import my deps, find Chromium, and setup my base command. I've found that Chromium can be under two different names, chromium and chromium-browser, depending on your container OS so the path check helps with that.

This example also makes use of Django's default_storage functionality to store files in the proper location making this work with a variety of different storage options.

Note that I do use Chromium in a Docker container for this so I have a flag that disables Chromium sandboxing since that's the current recommended way of running Chromium inside Docker. You should absolutely remove this flag if you aren't running Chromium in a container.

I then make two helper functions for saving images to storage and running our Chromium command, you can modify this to save to the OS directly if you don't want to use Django's storage system.

Then create our two main functions for generating the actual screenshots, one for generating from a URL and one from generating from HTML directly. You'll also need to modify these slightly if you don't want to use Django's storage system.

You can now import these two functions anywhere you want to create a screenshot. As a quick example if you wanted to take a screenshot of my blog you'd run:

As a bonus if you wanted to generate a PDF you can add another function to do this very easily since Chromium supports CLI PDF generation.

You'd run this the exact same way as the generate_screenshot_from_url function.

That's all you need to generate screenshots and PDFs! I've found this to be much more consistent than using the various screenshot and PDF libraries available for Python, you also have a lot of control over Chromium with it's many CLI switches.


Related posts

Some posts in similar tags to this one.

EditorConfig website
Using EditorConfig to improve coding style consistency
EditorConfig has been around for almost a decade at this point. It is widely supported by many editors natively and many more with plugins.
Isaac Bythewood Isaac Bythewood
May 21, 2022
Caddy website
Caddy configuration for Django with some sensible defaults
Caddy is a great web server with sensible defaults but there a few things that I need to configure to have perfect synergy with Django.
Isaac Bythewood Isaac Bythewood
June 04, 2022
CodeMirror website
Using CodeMirror to show formatted code in Wagtail
Going through all the steps to use CodeMirror with Wagtail to show formatted code on the frontend of your site.
Isaac Bythewood Isaac Bythewood
June 11, 2022
© 2024 Isaac Bythewood. Some rights reserved.