devenh
Posts: 32
Joined: Fri Jun 05, 2015 9:11 pm

Screen capture - calling scrot from Python 3 not working

Wed Aug 16, 2017 3:39 pm

I'm trying to do a simple and easy screen capture from Python (meaning I don't have to install any additional packages). Executing scrot from the command window works fine, but calling it from Python fails:

Code: Select all

result = os.system('scrot')
Is there some other way to call scrot that will work? Or is there a simple screenshot alternative to scrot? Using Python 3 and the latest Raspbian.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Screen capture - calling scrot from Python 3 not working

Wed Aug 16, 2017 3:50 pm

When you say "it fails", can you tell us what happens and what you expect to happen.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

devenh
Posts: 32
Joined: Fri Jun 05, 2015 9:11 pm

Re: Screen capture - calling scrot from Python 3 not working

Wed Aug 16, 2017 4:54 pm

By 'fail' I mean the result code returned is zero and the png file was not created in \home\pi as it did when run from the command prompt.
However, based on further testing, it looks like scrot doesn't return a result code, so the code returned by os.system should be ignored.

Furthermore, I mistakenly assumed that the png file would always be placed in \home\pi. This is not the case as I found the output in my program file.

So the example code does work, just no need to check the result code and you have to look in the right place to find the output file.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Screen capture - calling scrot from Python 3 not working

Wed Aug 16, 2017 8:43 pm

Does scrot allow you to specify a filename for the image? If so, you could pass that too and save the file exactly where you want. e.g.

Code: Select all

import subprocess

image_file = "/home/pi/screenshot.png"

subprocess.call(["scrot", image_file])
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

devenh
Posts: 32
Joined: Fri Jun 05, 2015 9:11 pm

Re: Screen capture - calling scrot from Python 3 not working

Thu Aug 17, 2017 1:19 pm

Does scrot allow you to specify a filename for the image?
Yes.

Code: Select all

scrot filename

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Screen capture - calling scrot from Python 3 not working

Thu Aug 17, 2017 1:40 pm

Great. So, using the example above, you should be able to save files in whichever folder you want.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Return to “Python”