Page 1 of 1

file saved as timestamp

Posted: Sat Feb 29, 2020 1:06 am
by n1zut
I'm having trouble getting my picamera to save a photo filename as a timestamp (Year/Month/Day/Hour/Minute/Second/and possibly millisecond).
My code in its entirety so far:

Code: Select all

import RPi.GPIO as GPIO
from time import sleep
from picamera import PiCamera
import datetime
import time

camera = PiCamera()
timestamp = datetime.datetime.now().strftime("%m%d%Y%H%M%S")

GPIO.setmode(GPIO.BOARD)
GPIO.setup(16, GPIO.IN)

while (1):

    if GPIO.input(16) == True:
        camera.start_preview()
        for i in range(3):

            camera.capture_continuous('/home/pi/Desktop/VehiclePics/(+timestamp+).jpg' % i)

        camera.stop_preview()

    elif GPIO.input(16) == 0:
        pass

but I keep getting this error message:

Traceback (most recent call last):
File "/home/pi/new.py", line 22, in <module>
camera.capture_continuous('/home/pi/Desktop/VehiclePics/(+timestamp+).jpg' % i)
TypeError: not all arguments converted during string formatting


Any help would be much appreciated!

Re: file saved as timestamp

Posted: Sat Feb 29, 2020 9:20 am
by gordon77
try

Code: Select all

camera.capture_continuous('/home/pi/Desktop/VehiclePics/' + timestamp + '.jpg' % i)

Re: file saved as timestamp

Posted: Sat Feb 29, 2020 9:25 am
by DirkS
gordon77 wrote:
Sat Feb 29, 2020 9:20 am
try

Code: Select all

camera.capture_continuous('/home/pi/Desktop/VehiclePics/' + timestamp + '.jpg' % i)
Doesn't it need a placeholder for 'i' too?

Re: file saved as timestamp

Posted: Sat Feb 29, 2020 10:12 am
by gordon77
Maybe.

I don't think it will work as it is just calling capture_continuous several times.

maybe try..

Code: Select all

import RPi.GPIO as GPIO
from time import sleep
from picamera import PiCamera
import datetime
import time

camera = PiCamera()

GPIO.setmode(GPIO.BOARD)
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

while True:
    if GPIO.input(16) == True:
        camera.start_preview()
        timestamp = datetime.datetime.now().strftime("%m%d%Y%H%M%S")
        for i in range(3):
            camera.capture('/home/pi/Desktop/VehiclePics/' + timestamp + '_' + str(i) + '.jpg')
        camera.stop_preview()

Re: file saved as timestamp

Posted: Sat Feb 29, 2020 10:31 pm
by n1zut
Thanks gordon77!

That worked great. I have been trying to attack this for weeks now!