Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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'
share|improve this question

1 Answer 1

up vote 0 down vote accepted

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.

share|improve this answer
    
This looks like it should work and I have tested it but it now says: TypeError: Can't convert 'int' object to str implicitly –  Mad_man111 Aug 31 '14 at 13:27
    
I assume when I fix that it should function correctly? –  Mad_man111 Aug 31 '14 at 13:30
    
Whoops, missed the conversion from int to string. You will need to use str(counter) to convert the int to string in order to do the string concatenation. Updated my answer, so now it should really work. –  ConcurrentHashMap Aug 31 '14 at 13:30
1  
It works perfectly now! Thanks –  Mad_man111 Aug 31 '14 at 13:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.