I'm using Gphoto2 to take pictures on a DSLR. As its based on bash commands I tried to use subprocess.communicate
but it freezes after the camera takes a picture.
If I try the gphoto2 --capture-image-and-download
in the terminal it takes less than 2 seconds. I'm working on a Raspberry Pi.
Code:
import subprocess
class Wrapper(object):
def __init__(self, subprocess):
self._subprocess = subprocess
def call(self,cmd):
p = self._subprocess.Popen(cmd, shell=True, stdout=self._subprocess.PIPE, stderr=self._subprocess.PIPE)
out, err = p.communicate()
return p.returncode, out.rstrip(), err.rstrip()
class Gphoto(Wrapper):
def __init__(self, subprocess):
Wrapper.__init__(self,subprocess)
self._CMD = 'gphoto2'
def captureImageAndDownload(self):
code, out, err = self.call(self._CMD + " --capture-image-and-download")
if code != 0:
raise Exception(err)
filename = None
for line in out.split('\n'):
if line.startswith('Saving file as '):
filename = line.split('Saving file as ')[1]
return filename
def main():
camera = Gphoto(subprocess)
filename = camera.captureImageAndDownload()
print(filname)
if __name__ == "__main__":
main()
If I exit I get this:
Traceback (most recent call last):
File "test.py", line 39, in <module>
main()
File "test.py", line 35, in main
filename = camera.captureImageAndDownload()
File "test.py", line 22, in captureImageAndDownload
code, out, err = self.call(self._CMD + " --capture-image-and-download")
File "test.py", line 11, in call
out, err = p.communicate()
File "/usr/lib/python2.7/subprocess.py", line 799, in communicate
return self._communicate(input)
File "/usr/lib/python2.7/subprocess.py", line 1409, in _communicate
stdout, stderr = self._communicate_with_poll(input)
File "/usr/lib/python2.7/subprocess.py", line 1463, in _communicate_with_poll
ready = poller.poll()
KeyboardInterrupt
Any ideas?