In this tutorial, I will show you how to add the date and time automatically to the photos captured by Raspberry Pi as the timestamp. In this way, you can turn your Raspberry Pi camera to a full-fledged security surveillance camera.
A date in Python is not a data type of its own, but we can import a module named datetime to work with dates as date objects.
The date contains year, month, day, hour, minute, second, and microsecond.
The datetime module has many methods to return information about the date object.
For instance, the strftime() function formats a local time and/or date according to locale settings.
- Include the required modules.
- Initiate the camera module with pre-defined settings - resolution, and framerate.
- In the capture_photo() function, add the date and time as the timestamp on the captured photos by executing the annotate_text() function.
- Get the current date and time as the timestamp to generate unique file names.
- Implement the timestamp to the file name.
- Execute the capture_photo() function.
-------------- Python -------------- from picamera import PiCamera from time import sleep import datetime # Initiate the camera module with pre-defined settings. camera = PiCamera() camera.resolution = (640, 480) camera.framerate = 15 def capture_photo(file_capture, text): # Add date as timestamp on the generated files. camera.annotate_text = text # Capture an image as the thumbnail. sleep(2) camera.capture(file_capture) print("\r\nImage Captured! \r\n") # Get the current date as the timestamp to generate unique file names. date = datetime.datetime.now().strftime('%m-%d-%Y_%H.%M.%S') capture_img = '/home/pi/test_' + date + '.jpg' capture_photo(capture_img, date)
You can inspect my electronics project in which the mentioned method is used to develop a surveillance camera from here.