Take screenshot of the entire desktop using Mathematica

21

8

Is it possible to get a screenshot of the entire desktop directly in Mathematica?

I am on Ubuntu running Mathematica 8. For some reason GUIScreenShot[] fails. But something like GUIScreenShot[{{0, 500}, {0, 500}}] works

It is strange that in the reference

GUIScreenShot[]

is given as an example (last one), with the caption Capture the entire screen:. But when I copy it and paste it in a notebook, it expects a parameter! (I have included the code Needs["GUIKit`"])

I get the error as shown in this image

Screenshot of the image

my account_ram

Posted 2012-08-11T22:46:15.417

Reputation: 2 068

2I am trying to automate a simple game (!). So grabbing it using an external program, then saving it as a file and importing the file again from within Mathematica is a tedious job (if not realtime). Also I am not sure if I can pipe the output of a program as input to Mathematica (I could not find a way to get it done with Run[] command! - i.e. the piping large output from external program) – my account_ram – 2012-08-11T23:16:01.617

GUIScreenShot[] works here (Win XP) – Dr. belisarius – 2012-08-11T23:34:11.103

GUIScreenShot[] doesn't work for me (Arch Linux), it spits this error message on the terminal. Looks like a Linux issue... – Renan – 2012-08-12T00:14:41.157

It works fine on my win7_x64 and mma8.0.4 (although a syntax indicator). Didn't know this function before. Very helpful! Thanks and +1!

– Silvia – 2012-08-12T06:52:24.363

GUIScreenShot[] works in version 7 under Windows 7. – Mr.Wizard – 2012-08-12T07:55:44.317

Works also in version 8 under XP Pro SP3. – stevenvh – 2012-08-12T12:59:01.843

Answers

16

GUIScreenShot[] (without arguments) works perfectly fine on my Mac, although the front end syntax highlighter indicates a missing argument. As a workaround, you can explicitly give it the dimensions of your screen so that all of it is captured. Here's how you can programmatically get your screen size and use it:

Needs["GUIKit`"]
GUIScreenShot["FullScreenArea" /. Flatten@SystemInformation["Devices", "ScreenInformation"]]

rm -rf

Posted 2012-08-11T22:46:15.417

Reputation: 85 395

1This does not seem to work on Ubuntu .. Can any1 confirm this ... – my account_ram – 2012-08-12T00:58:40.250

@myaccount_ram I'm having the same problem, but with Arch Linux. – Renan – 2012-08-12T04:00:38.470

@myaccount_ram It doesn't work on Fedora. – b.gates.you.know.what – 2012-08-12T07:34:09.090

I got ** Raster data contains 0 values per pixel; 3 or 4 are required with ColorFunction->RGBColor** under Linux 64. – Fred Kline – 2012-08-12T20:39:19.083

1@everyone: The issue seems to be something related to linux and not Mathematica. I would've helped if I could, but I don't have a license to use on my Linux machine. – rm -rf – 2012-08-13T01:53:55.717

So many uses. Look: Needs["GUIKit`"] Dynamic[out] FixedPoint[(Pause[1]; out = GUIScreenShot[]) &, 0] – amr – 2013-05-06T01:15:06.667

7

In v11.2 you can just use CurrentScreenImage[].

user5601

Posted 2012-08-11T22:46:15.417

Reputation: 4 641

7

A method from Kuba's answer, no GUIKit` is needed. Tested on Mathematica 10.1.

Module[{masknb, img},
       masknb = CreateWindow[{}, WindowSize -> Full, Visible -> False];
       img = MathLink`CallFrontEnd[FrontEnd`NotebookImage[masknb]];
       NotebookClose[masknb];
       img
      ]

We can even make a fairly fast screen recorder from it:

Clear[maskWindow]
maskWindow[width_, height_, leftMarg_, topMarg_] :=
    CreateWindow[{},
        WindowSize -> {width, height},
        WindowMargins -> {{leftMarg, Automatic}, {topMarg, 0}},
        Visible -> False]

Clear[screenSnapshot]
screenSnapshot[window_] := 
    MathLink`CallFrontEnd[FrontEnd`NotebookImage[window]]

screenRecord = {};
recordingFlag = True;
Column[{
        "Click to stop recording:",
        Toggler[Dynamic[recordingFlag],
            {True -> Framed["Recording"], False -> Framed["Stopped  "]}
            ]
        }]

Module[{masknb},
        masknb = maskWindow[500, 300, 300, 200];
        While[recordingFlag,
            screenRecord = Join[screenRecord, {screenSnapshot[masknb]}]
            ];
        NotebookClose[masknb];
        ];
screenRecord = Image /@ screenRecord;
ListAnimate[screenRecord]

MMA screen recorder

Silvia

Posted 2012-08-11T22:46:15.417

Reputation: 25 336

Extremely Fast!!!!! Very impressive answer~ xie xie la~ but still, are there faster methods? – Wjx – 2016-08-25T14:25:31.960

@Wjx Thanks! For better performance, I'd go win API + NETLink. – Silvia – 2016-08-26T08:51:34.347

5

Confirming @R.M's answer (WinXP, Mac OSX 10.8)

enter image description here

Dr. belisarius

Posted 2012-08-11T22:46:15.417

Reputation: 112 848

1why not a comment? Also, why are you hiding your porn tabs? ;) – rm -rf – 2012-08-11T23:44:01.063

@rm -rf The porn tabs are there ... look again – Dr. belisarius – 2012-08-11T23:45:14.977

@rm -rf Re: Why not a comment. I like the conceptual Droste Effect produced by the image of a question asking for a screen cap on the answer's screen cap.

– Dr. belisarius – 2012-08-11T23:59:07.600

Doesn't work properly with more than one monitor. It only captures the primary monitor even if the MMA windows are on the other screen(s). – s0rce – 2012-08-12T03:28:05.993

1@belisarius Nest[GUIScreenShot[], "", 10] – s0rce – 2012-08-12T03:29:39.563

1@belisarius It's not a Droste effect until it is. – rm -rf – 2012-08-12T11:57:25.970

1The Droste Effect: Dynamic[Export["C:\\droste.png", GUIScreenShot[]]; Style[GUIScreenShot[], Magnification -> 1], UpdateInterval -> 3]. When the Dynamic object is not visible, it is not updated (it is sufficient just to reduce the Mathematica window). – Alexey Popkov – 2012-08-12T18:33:17.073

4

Using JLink,you can do this

Needs["JLink`"];
InstallJava[];
robotclass = JavaNew["java.awt.Robot"];
LoadJavaClass["javax.imageio.ImageIO"];
LoadJavaClass["java.awt.Toolkit"];
screenSize = Toolkit`getDefaultToolkit[]@getScreenSize[];
image = robotclass@createScreenCapture[JavaNew["java.awt.Rectangle", 0, 0, screenSize@width, screenSize@height]];
ImageIO`write[image, "png",JavaNew["java.io.File", FileNameJoin[{Directory[], "test.png"}]]];
UninstallJava[]

Import["test.png"]

enter image description here

partida

Posted 2012-08-11T22:46:15.417

Reputation: 6 452