Monday, May 23, 2016

PI Critter Cam Day 5: Picture File Names with the Date and Time

Today, my goal is expand on the picture taking, have it take a picture with a date and time stamp file name and also delete pictures that have reached a certain age.  I'm hoping there will be lots of critters to take pictures of, but my micro SD card is only 8gb.

Since I can't store my pics on the PI long term, my ultimate goal is to have the pictures automatically post to a Twitter handle where they can be stored and archived (and shared).  That'll be further down the road.

So:

  • Capture a picture with a date and time stamp to make it unique and easier to manage.
  • Delete pictures that are older than 2 weeks.
We'll just do both in the same process for now.  It might not be ideal, but I think it'll be alright.  Trying to keep it simple.

We'll use the Python Time package for collecting the time.  

It's actually pretty straightforward manipulating the time into a string for a file name using the strftime function:

time.strftime('%Y_%m_%d_%H_%M_%S')

I can just stick it in like so.  I'm also going to add a folder so everything won't write to a root directory:


#Needed packages...
import picamera
import time

#create object and initialize camera.
cam = picamera.PiCamera()

#take picture!
cam.capture('/home/pi/Pictures/critter_cam/' + time.strftime('%Y_%m_%d_%H_%M_%S') + '.jpg')

Note:  you have to create the actual folder via the File Manager.  Python will not create it automatically by referencing it in the file name.

And it seems to work.  Here's the pic in the folder with the date and time stamp.


We ran out of time regarding deleting old files.  That'll have to wait until next time.

No comments: