Mohka has asked for the wisdom of the Perl Monks concerning the following question:

Any body know how to take a screen shots of multiple web pages and save it as a jpeg file using Perl? on Linux platform

Replies are listed 'Best First'.
Re: Screenshots of web pages
by shmem (Chancellor) on Sep 04, 2008 at 10:25 UTC

    Sure.

    my $i = 0; for my $url (@urls) { my $outfile = sprintf "screenshot-%02d.png", ++$i; system 'firefox', $url; system "xwd | xwdtopnm | pnmtopng > $outfile"; }

    After the page is fully loaded, click the firefox window. Adding e.g. gimp to that loop to convert the PNG file to JPEG is left as an exercise to the reader ;-)

Re: Screenshots of web pages
by moritz (Cardinal) on Sep 04, 2008 at 10:12 UTC
    This is not a trivial task to do in perl. However there are other tools, for example khtml2png, which uses the khtml rendering engine (the one that is used in the browser Konqueror, and I think it has some common code with webkit; not sure though).

    You can also search for thumbshot, I'm sure there are other solutions out there.

Re: Screenshots of web pages
by wazoox (Prior) on Sep 04, 2008 at 11:04 UTC
    You could use http://browsershots.org/ to take care of this. An additional benefit is that you can choose the browsers and platforms to use. You can automate queries to browsershot.org with WWW::Mechanize, for instance.
Re: Screenshots of web pages
by zentara (Cardinal) on Sep 04, 2008 at 15:55 UTC
    Well switching bewtween multiple web pages can be tricky, because how do you know it was succesfully loaded before you take the screenshot? But here is a script that works on linux, the one caveat is that you must run the script from the same virtual desktop that your browser is on.
    #!/usr/bin/perl use warnings; use strict; use Image::Magick; use X11::WMCtrl; use Data::Dumper; my $wmctrl = X11::WMCtrl->new; printf("window manager is %s\n", $wmctrl->get_window_manager->{name}); my @windows = $wmctrl->get_windows; my $wid; # x window id my $host; # virtual desktop foreach my $app(@windows){ my $title = $app->{title}; if( $title =~ /Mozilla/i){ print "$title\n"; print Dumper(\$app),"\n\n"; $wid = $app->{id}; $host = $app->{host}; } } $wmctrl->switch($host); #must be on same virtual desktop as the browser #works my $blob = `import -window $wid jpg:`; #my $blob = `import -`; #postscript produced #print "$blob\n"; # now $blob is in memory and you can do what you # want with it. my $output = Image::Magick->new(magick=>'jpg'); $output->BlobToImage( $blob ); $output->Resize(geometry=>'160x120'); $output->Write( $0.'.jpg'); exit; # png works too

    I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: Screenshots of web pages
by dreadpiratepeter (Priest) on Sep 04, 2008 at 16:07 UTC
    I spent about 4 hours looking for a solution to this the other day, with no luck.
    I need a command-line driven answer that doesn't require a browser or graphical environment.
    I looked at the gecko and webkit engines and all over google, but i had no success.
    I would think that it wouldbe easy to implement in one of the content engines, but they all seem to be tied to rendering to a display.


    -pete
    "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."
      As mentioned above, you could build one with WWW::Mechanize and browsershots.org http://browsershots.org/
      Then share your work by putting it on CPAN - I'd use it!
      headless x server is not an option?