Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically take ScreenShot of Desktop in Ruby?

I asked this question about taking a picture of a webpage programmatically, and I've downloaded and got webkit2png working (taking pictures of HTML pages like blogs and whatnot). So cool, thanks for showing me that!

Now I would like to start doing more, like being able to take pictures of Flash websites after they have loaded, and of my desktop.

Is it possible to take pictures of Flash websites with webkit2png (considering you might have to wait a few seconds for it to load)?

But the main question is, how do I programmatically take pictures of the desktop? That would allow me to have much more control over what's going on.

like image 855
Lance Avatar asked Nov 14 '09 08:11

Lance


3 Answers

You can use xwd(1) to take a screenshot of the root window:

xwd -display :0 -root|xwdtopnm |pnmtopng > $1
like image 122
codehead Avatar answered Sep 17 '22 16:09

codehead


This is an awesome question!

A couple of years ago I has to work on a similar project. I found a library, called watir, you can use to control system browsers from Ruby. At the time I checked, it wasn't really reliable in a Linux environment, but right now it seems to be pretty solid.

Here's a couple of links:

  • http://90kts.com/blog/2008/capturing-screenshots-in-watir/
  • http://www.marekj.com/wp/2008/04/desktop-screenshots-with-watir-win32screenshot-and-rmagick/
  • http://clearspace.openqa.org/thread/13949
  • http://wiki.openqa.org/display/WTR/FAQ#FAQ-HowdoItakescreenshotsandappendtoaWordfile%3F

I have never tried this solution so I would be really happy if you can write here a feedback if you decide to go with Watir. All the examples targets a Windows server, I didn't found a valid tutorial using a Linux + Firefox environment.

like image 23
Simone Carletti Avatar answered Sep 16 '22 16:09

Simone Carletti


You can use Watir WebDriver like this:

$ irb
irb(main):001:0> require 'watir-webdriver'
=> true
irb(main):002:0> browser = Watir::Browser.new
=> #<Watir::Browser:0x136da92fad77d562 url="about:blank" title="">
irb(main):003:0> browser.goto 'http://stackoverflow.com/questions/1733715/programmatically-take-screenshot-of-desktop-in-ruby'
=> "http://stackoverflow.com/questions/1733715/programmatically-take-screenshot-of-desktop-in-ruby"
irb(main):004:0> browser.screenshot.save 'screenshot.png'
=> #<File:screenshot.png (closed)>
irb(main):005:0>

which seems to do the job rather well! I tested this on my Mac, but I'd be surprised if it didn't work well on Linux too.

like image 31
Matt Wallis Avatar answered Sep 20 '22 16:09

Matt Wallis