pythonterminalscreenshotchromebookcrostini

How to take screenshot of Chromebook in Linux command-line?


In Linux on my Chromebook, I am trying to take a screenshot using the command line, but nothing seems to be working. I have tried ImageMagick...

sudo apt-get install imagemagick

import -window root filename.png
# "unable to read X window image 'root'"

import filename.png
# "unable to grab mouse '': No such file or directory"

And I have also tried scrot...

sudo apt-get install scrot

scrot -u filename.png
#"BadDrawable (invalid Pixmap or Window parameter)"

I tried several python methods, but the only one that I could successfully install and get to run without errors was pyscreeze (which uses scrot), and it produced nothing but a blank, black image, which is useless to me.

In terms of other python methods, I tried pyautogui which failed to install, telling me "Failed building wheel for Pillow." I tried pyscreenshot, which installed and ran but told me "All backends failed." I also tried ImageGrab, which told me "ImageGrab is MacOS and Windows only."

Does anyone know of a way to make this work?

Thanks in advance!


Solution

  • Javascript solution

    Here is a way to take a screenshot of any part of the chromebook's screen, a chrome window, or a chrome tab.

    Paste this into your browser console to take a screenshot.

    async function takeScreenshot(format = "image/png") {
      const stream = await navigator.mediaDevices.getDisplayMedia({video:true});
    
      const video = document.createElement("video");
      video.srcObject = stream;
      video.muted = true;
      await video.play();
    
      const canvas = document.createElement("canvas");
      canvas.width = video.videoWidth;
      canvas.height = video.videoHeight;
      const context = canvas.getContext("2d");
    
      context.drawImage(video, 0, 0);
    
      const blob = await new Promise(resolve => canvas.toBlob(resolve, format));
    
      stream.getTracks().forEach(track => track.stop());
    
      return blob;
    }
    
    function download(blob, name = "Untitled") {
      const elt = document.createElement("a");
      elt.download = name;
      elt.href = URL.createObjectURL(blob);
      elt.click();
      URL.revokeObjectURL(elt.href);
    }
    
    takeScreenshot("image/png").then(blob => download(blob, "Screenshot.png"));