How to take screenshot of an X11 based GUI from a text terminal (such as tty1)?
Solution 1
Here's what worked for me.
chvt 7
DISPLAY=:0 import -window root "$HOME/Pictures/screenshot.png
chvt 1
Solution 2
In addition to the existing answers, to display the screenshot in ASCII in the terminal:
xwd -root -display :0 | convert - jpg:- | jp2a - --colors
(Requires x11-apps
for xwd
, imagemagick
for convert
and jp2a
.)
Solution 3
Here is solution using xwd
, which is is available in almost all Xorg installations:
xwd -root -out screenshot.xwd
The screenshot.xwd
file can be opened with GIMP:
For more info see http://www.x.org/wiki/UserDocumentation/GettingStarted/.
Solution 4
Try using shutter
DISPLAY=:0 shutter -f -e -n -o "$HOME/Pictures/screenshot.png"
Import won't work, because the root window is not in use. Compositing "bypasses" it (each window is rendered in its own buffer then multiplexed to the display, instead of the old way, where they were all rendered on the "root" window).
In addition you need to make sure the VTY is active before you take the screenshot. So for example,
#!/bin/bash
X :1 &
export DISPLAY=:1
sleep 10 # give time for X to start
gedit &
sleep 10 # give time for app to do something
chvt 8 # ensure the X display is active
shutter -f -e -n -o "$HOME/Pictures/screenshot.png" # screenshot
killall xorg # terminate X
Related videos on Youtube
Comments
-
Yatharth Agarwal over 1 year
I tried using
DISPLAY=:0 import -window root "$HOME/Pictures/screenshot.png"
(import
is part of the ImageMagick suite), but that just displays a black screen.I want to do this as I wanted to automate opening an app and taking screenshots of it in different languages, and to change languages, I need to restart lightdm. therefore, run on a tty so your script continues running...
-
Yatharth Agarwal over 11 yearsNope, still giving a black screen :(
-
coteyr over 11 yearsAre you in the X session (on a monitor) when you do this. I just tried and it worked quite well. You will need to be logged in, and have the X session in the active VTY.
-
Yatharth Agarwal over 11 yearsI want a script launched in the tty to be able to do this.
-
coteyr over 11 yearsYes, but do you switch to the X session before taking the screen shot.
X :0 & sleep 50 && sudo chvt 6 && program & sleep 5 && screenshot
(notice totally fake script). -
coteyr over 11 yearsAlso just to make sure were talking the same thing you want VT0 (or something) not a screen shot of a text console. So your running the script from a TTY trying to screen shot X (VTY). Is that correct?
-
Yatharth Agarwal over 11 yearsYeah. If you've tested the script and it work fine, comment and I'll accept your answer. +1 for answering!
-
coteyr over 11 yearsThe script works fine.
-
Yatharth Agarwal almost 10 yearsNot exactly relevant, but cool addition.
-
gregn3 almost 8 years
xwd -root -display :0 | convert - jpg:- > screenshot.jpg
-
Bass about 6 yearsModern ImageMagick versions often can't infer the
xwd
format from magic numbers, so it's better to specify the input format explicitly:xwd -root -display :0 | convert xwd:- jpg:- | jp2a - --colors
-
Sathiya Narayanan over 5 yearsFor me:
xwd -name "$win_name" | convert xwd:- /tmp/"$win_name".png
, wherewin_name=$(xwininfo -tree -root | grep 'The Window Name Im Looking For' | awk -F\" '{print $2}')