How to take screenshot of an X11 based GUI from a text terminal (such as tty1)?

32,351

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
Share:
32,351

Related videos on Youtube

Yatharth Agarwal
Author by

Yatharth Agarwal

The internet remembers.

Updated on September 18, 2022

Comments

  • Yatharth Agarwal
    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
    Yatharth Agarwal over 11 years
    Nope, still giving a black screen :(
  • coteyr
    coteyr over 11 years
    Are 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
    Yatharth Agarwal over 11 years
    I want a script launched in the tty to be able to do this.
  • coteyr
    coteyr over 11 years
    Yes, 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
    coteyr over 11 years
    Also 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
    Yatharth Agarwal over 11 years
    Yeah. If you've tested the script and it work fine, comment and I'll accept your answer. +1 for answering!
  • coteyr
    coteyr over 11 years
    The script works fine.
  • Yatharth Agarwal
    Yatharth Agarwal almost 10 years
    Not exactly relevant, but cool addition.
  • gregn3
    gregn3 almost 8 years
    xwd -root -display :0 | convert - jpg:- > screenshot.jpg
  • Bass
    Bass about 6 years
    Modern 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
    Sathiya Narayanan over 5 years
    For me: xwd -name "$win_name" | convert xwd:- /tmp/"$win_name".png, where win_name=$(xwininfo -tree -root | grep 'The Window Name Im Looking For' | awk -F\" '{print $2}')