Script to capture a screenshot (with Chromium) (Command-line)

Introduction

I wanted to take screenshots of about 15 Websites, but didn’t want to install any plugins as I wouldn’t use them often. I saw a lot of how-to’s using import from Imagemagick but they capture the whole screen, not clearly what I was looking for.

Using chromium, xwininfo (from X) and import, I made this bash script which captures only the webpage. See below for the Source code.

How it works

Upon calling chromium-capture:

  • it finds every running chromium browser using xwininfo,
  • gets the window’s id of each browser’s currently opened tab (because hidden tabs cannot be accessed using xwininfo/import)
  • takes a screenshot of each webpages using import and the windows’ ids.
  • saves the screenshot in the current directy as chromium-WINDOWID.png (with WINDOWID = id of the respective window)

You get a screenshot of only the website. All of the arguments and options given to chromium-capture will be used as arguments and options when calling import.

Usage Examples

Every argument or option given to chromium-capture will be given to import when called with every capture.

#do a simple capture
/path/to/chromium-capture

#capture and resize to a width of 300px
/path/to/chromium-capture -resize 300

#capture and rotate the image 10degres
/path/to/chromium-capture -rotate 10

#resize to a maximum width of 550px and reduce the number of colors to 16
/path/to/chromium-capture -resize 550 -colors 16

Source Code

This code is licensed under the GPLv3.
You can download the source code from here (md5) or copy it from below.

chromium-capture

#!/bin/bash
#
#    Version: 201001081150
#
#    Copyright © 2010 Arnaud Soyez (contact me at codealpha.net)
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see .

echo '`import` will be called with these args: '$@
echo

CHROMIDS=$(xwininfo -root -children\
 | grep ' - Chromium": ("chromium-browser" "Chromium-browser")'\
 | awk '{ print $1 }')

function getViewableId ()
{
  WINID=$(xwininfo -id $1 -children\
 | egrep -i '^ +0x[0-9A-F]+'\
 | fgrep -v '1x1+'\
 | awk '{ print $1 }'\
 | xargs -n 1 xwininfo -id\
 | egrep '(xwininfo|Map State)'\
 | sed 'N;s/\
  Map//'\
 | grep IsViewable\
 | awk '{ print $4 }')

  echo $WINID
}

for CHROMID in $CHROMIDS; do
  echo -n 'Capturing window id '$CHROMID' ...'
  FILENAME=chromium-$CHROMID.png
  import -window $( getViewableId $CHROMID ) $@ "$FILENAME"
  echo ' saved as '$FILENAME
done

echo 'Done'

One Reply to “Script to capture a screenshot (with Chromium) (Command-line)”

  1. This works great, but only for visible windows/tabs. If I have one window overlapping another, and screenshot the window in the back, I get a big black area where the background window was covered by the foreground window. Have you seen this behavior?

Leave a Reply