How to take screen screenshot in Python?

3

I would like to make a program that screens the screen (screen shot) in Python.

I found the following code:

import pyscreenshot as ImageGrab

if __name__ == "__main__":
    # fullscreen
    im=ImageGrab.grab()
    im.show()

The code does nothing (apparently crashes)!

What does it mean: if __name__ == "__main__": ?

Using Linux BackBox

    
asked by anonymous 01.08.2016 / 03:27

1 answer

3

Firstly if you have not installed the module, install :

~$ sudo apt-get install python-pip && pip install pyscreenshot

Obs : pip is a tool for installing modules of Python.

To use pyscreenshot just do:

import pyscreenshot as ImageGrab

def main():
    imagem = ImageGrab.grab()
    imagem.save('screenShot1.jpg', 'jpeg')

if __name__ == "__main__":
    main()

The code above will generate a JPEG image and save it as screenShot1.jpg in the same script directory.

Your second question is answered here .

    
01.08.2016 / 05:03