4 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.

Capturing screenshots with Chromium using Python
Author
Isaac Bythewood Isaac Bythewood
2022-08-06

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 its many CLI switches.


Related posts

Some posts in similar tags to this one.

Code formatting a Python project in 2022
Code formatting a Python project in 2022
For those who want a quick solution without reading all of PEP 8. The Black Python module has a fully automated solution for you.
Isaac Bythewood Isaac Bythewood
2022-07-30
Finding broken external links on websites using Scrapy
Finding broken external links on websites using Scrapy
Broken links are a problem for any content driven website as it ages, find them quickly and easily with Scrapy.
Isaac Bythewood Isaac Bythewood
2022-07-23
Make your own new tab browser extension in 50 lines of code
Make your own new tab browser extension in 50 lines of code
There are plenty of home page and new tab replacement extensions on the Chrome Web Store that you could use, but why not make your own if it's easy?
Isaac Bythewood Isaac Bythewood
2022-07-09