I have been trying to set up my raspberry pi to auto take pictures every 5 seconds and save the file as image1,image2 etc. This is the Python code I have been trying:
import time
import picamera
counter = 0
with picamera.PiCamera() as camera:
for each in range(5):
counter = counter + 1
camera.start_preview()
time.sleep(5)
camera.capture("/home/pi/python/Pictures/image",counter,".jpg")
camera.stop_preview()
But every time I run this I get this Error:
Traceback (most recent call last):
File "/home/pi/python/camera_repeated.py", line 9, in <module>
camera.capture("/home/pi/python/Pictures/image",counter,".jpg")
File "/usr/lib/python3/dist-packages/picamera/camera.py", line 1303, in capture
format = self._get_image_format(output, format)
File "/usr/lib/python3/dist-packages/picamera/camera.py", line 684, in _get_image_format
format[6:] if format.startswith('image/') else
AttributeError: 'int' object has no attribute 'startswith'
You're doing the string concatenation wrong. Strings in python should be concatenated with +
between them. You're using the ,
sign, that normally separates arguments in function calls.
The signature for PiCamera.capture
is defined as
capture(output, format=None, use_video_port=False, resize=None, splitter_port=0, **options)
So you will at first specify the output file as a string and then (optional) the format. If not specified a format, the format will be derived from the file extension of the given output file, so you can leave it empty here.
So the right call in line 9 should be:
camera.capture("/home/pi/python/Pictures/image" + str(counter) + ".jpg")
You're only giving in a string now. Before you gave in 3 params, with the format being the value of your counter
variable and the third (use_video_port
) with .jpg
. Internally, the library seems to test the given format for common known mime-types startswith('image/')
, but as you are giving in an integer, there is no such method startswith
. That resulted in the error.