kylerichards89
Posts: 98
Joined: Sun Aug 31, 2014 12:42 am

Timestamp in photo filename

Sun Aug 31, 2014 5:43 pm

Hello all, I have a quick question regarding the saving of a photo with the Picamera. Using the code:

Code: Select all

camera.capture('/home/pi/photobooth/1.jpg')
how would I get the file to be the "month_day_year_hour_minute_second.jpg" instead of 1.jpg. I've tried using the %d, %m, ect. but python doesn't understand without additional coding. Any and all help with be greatly appreciated!
Last edited by kylerichards89 on Wed Oct 29, 2014 6:05 pm, edited 2 times in total.

User avatar
kusti8
Posts: 3439
Joined: Sat Dec 21, 2013 5:29 pm
Location: USA

Re: Timestamp in photo filename

Sun Aug 31, 2014 9:05 pm

Code: Select all

import calendar #Not sure if this is correct, can't test it
import datetime

d = datetime.today()

date = calender.month_name(d.month) + "_" + str(d.day) + "_" + str(d.year) + "_" + str(d.hour) + "_" + str(d.minute) + "_" + str(d.second)

camera.capture("/home/pi/" + date + ".jpg"
Let me know if I have missed something!
There are 10 types of people: those who understand binary and those who don't.

kylerichards89
Posts: 98
Joined: Sun Aug 31, 2014 12:42 am

Re: Timestamp in photo filename

Mon Sep 01, 2014 12:31 am

Thanks for the reply! Unfortunately the code didn't work. :( I received the following error:
File "led.py", line 17, in <module>
d = datetime.today()
AttributeError: 'module' object has no attribute 'today'

User avatar
kusti8
Posts: 3439
Joined: Sat Dec 21, 2013 5:29 pm
Location: USA

Re: Timestamp in photo filename

Mon Sep 01, 2014 1:01 am

Updated version:

Code: Select all

import calendar #Not sure if this is correct, can't test it
import datetime

d = datetime.date.today()

date = calender.month_name(d.month) + "_" + str(d.day) + "_" + str(d.year) + "_" + str(d.hour) + "_" + str(d.minute) + "_" + str(d.second)

camera.capture("/home/pi/" + date + ".jpg"
There are 10 types of people: those who understand binary and those who don't.

kylerichards89
Posts: 98
Joined: Sun Aug 31, 2014 12:42 am

Re: Timestamp in photo filename

Mon Sep 01, 2014 1:20 am

I figured it out! It also uses less coding (so hopefully faster in the end). I used:

Code: Select all

import datetime
import time
date = datetime.datetime.now().strftime("%m_%d_%Y_%H_%M_%S")
     camera.capture("/home/pi/photobooth/"+ date + ".jpg")
Thanks for the help though! And for any references, I deleted the rest of the code for the sake of this post. Hopefully this can help someone else out!

liquidvisual
Posts: 1
Joined: Sun Aug 20, 2017 2:07 pm

Re: Timestamp in photo filename

Tue Jan 23, 2018 8:31 pm

Thank you for posting the solution!

ankit mishra
Posts: 2
Joined: Sun Dec 24, 2017 3:59 am

Re: Timestamp in photo filename

Wed May 30, 2018 1:45 pm

How can i get filename with milliseconds as well in addition to hours minutes and seconds. Thanx in advance

pcmanbob
Posts: 7077
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Timestamp in photo filename

Wed May 30, 2018 2:15 pm

ankit mishra wrote:
Wed May 30, 2018 1:45 pm
How can i get filename with milliseconds as well in addition to hours minutes and seconds. Thanx in advance
you need to add %f to the end of the existing line

Code: Select all

date = datetime.datetime.now().strftime("%m_%d_%Y_%H_%M_%S_%f")
which will give you

Code: Select all

05_30_2018_15_13_26_475374
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

RDS
Posts: 721
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: Timestamp in photo filename

Fri Jun 01, 2018 9:51 pm

I regularly use the date as part of a filename but I prefer to see the files stored in chronological order, so I use Year, Month, Day (rather than Month, Day, Year).

Return to “Python”