I am looking to take a screenshot of my screen on Google Colab for use in a screen recorded video, but as the the notebook runs in linux, I am not sure if this is possible.
My code from python on my device is below
from PIL import ImageGrab
img = ImageGrab.grab(bbox= None)
When run on Colab, it returns either OSError: X connection failed: error 5
or ImportError: ImageGrab is macOS and Windows only
.
Looking for other options, I first tried mss.mss()
, which returned $DISPLAY not set.
. I tried setting the display in the method and using os
, and neither of which worked.
I then tried gtk.gdk
, but I was not able to install and import it properly after trying several methods.
I also tried using pyautogui
, but after importing I was not able to connect to a display, and was returned either a DisplayConnectionError
or an OSError
.
Was there a way to fix any of the errors returned using the methods I listed? Or is there another method that I am not aware of to return the contents of a screen as an image in Colab?
First you need to setup a virtual monitor:
!pip install gym pyvirtualdisplay > /dev/null 2>&1
!apt-get install -y xvfb python-opengl ffmpeg > /dev/null 2>&1
DISPLAY_WIDTH = 1920
DISPLAY_HEIGHT = 1080
from IPython import display as ipythondisplay
from pyvirtualdisplay import Display
display = Display(visible=0, size=(DISPLAY_WIDTH, DISPLAY_HEIGHT))
display.start()
Then install pyscreenshot:
!pip install pyscreenshot
import pyscreenshot as ImageGrab
Finally grab the picture:
frame = np.array(ImageGrab.grab(bbox= None))
In ImageGrab.grab(bbox=(x, y, w, h))
you can specify the size of the screen you want to grab as well.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.