I have a function that needs to close the camera at the start and re-open it at the end. I know that to close it I do:
import picamera
camera = picamera.PiCamera()
camera.close()
How do I re-open it?
Maybe you could create a new camera instance. (Which may be the easiest solution)
camera = picamera.PiCamera()
Or, you could try to design your function to keep the camera open and then close it at the end of the program. Which can be automatically achieved using the with statement:
with picamera.PiCamera() as camera:
...do something...
But I am afraid that the API of picamera does not allow reopening the camera. You could also try to "hack" into picamera using the "private" methods. When you look at the code of close, you'll see:
for port in list(self._encoders):
self.stop_recording(splitter_port=port)
assert not self.recording
for overlay in list(self._overlays):
self.remove_overlay(overlay)
if self._preview:
self._preview.close()
self._preview = None
if self._splitter:
self._splitter.close()
self._splitter = None
if self._camera:
self._camera.close()
self._camera = None
exc, self._camera_exception = self._camera_exception, None
if exc:
raise exc
But that would require reopening several other objects such as _preview, _splitter and _camera (And they even are set to None). Which would probably be hard to do, unstable and not compatible between versions. So, DON'T DO THAT.