Using Python 2.7 on Raspberry Pi B+, I want to call the command "raspistill -o image.jpg" from Python and find using this is recommended:
from subprocress import call
call(["raspistill","-o image.jpg"])
However, this doesn't work since the image.jpg isn't created although outside Python,
raspistill -o
does create the file. Next try is to first create the image file and writing to it.
f = open("image.jpg","w")
call(["raspistill","-o image.jpg"], stdout = f)
Now the image file is created, but nothing is written to it: its size remains 0. So how can I get this to work?
Thank you.
You are passing -o image.jpg
as a single argument. You should pass them like two. Here is how:
call(["raspistill", "-o", "image.jpg"])
The way you did it it's like calling raspistill "-o image.jpg"
from the command line, which will likely result in an error.
First, you're creating and truncating the file image.jpg
:
f = open("image.jpg","w")
Then you're sending raspistill
's stdout to that same file:
call(["raspistill","-o image.jpg"], stdout = f)
When you eventually get around to close
-ing the file in Python, now image.jpg
is just going to hold whatever raspistill
wrote to stdout. Or, if you never close
it, it'll be that minus the last buffer, which may be nothing at all.
Meanwhile, you're also trying to get raspistill
to create a file with the same name, by passing it as part of the -o
argument. You're doing that wrong, as Ionut Hulub's answer explains. Some programs will take "-o image.jpg"
"-oimage.jpg"
, and "-o", "image.jpg"
as meaning the same thing, some won't. But, even if this one does, at best you've now got two programs fighting over what file gets created and written as image.jpg
.
If raspistill
has an option to write the still to stdout, then you can use that option, together with passing stdout=f
, and making sure to close
the file. Or, if it has an option to write to a filename, then you can use that option. But doing both is not going to work.
If you don't know how to split the command, you can use shlex.split
. For example,
>>> import shlex
>>> args = shlex.split('raspistill -o image.jpg')
>>> args
['raspistill', '-o', 'image.jpg']
>>> call(args)
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.