Tutorial 14: Add a Timestamp to your Picture

Okay this tutorial isn't specific to just the Raspberry Pi. Any Linux machine can do this. In this tutorial I show you how to install Image Magick on your Raspberry Pi and use the software via Bash commands (in Python 3) to append a time stamp to the pictures you take with the Pi Camera.

This tool allows you to append any text to the images you want. I am doing a time and date stamp. You could also apply this to situations where you keep track of other values such as temperature, speed, position, whatever is relevant to your project.

This tutorial is a little older (published in 2015!). The main principles are still relevant, so I haven't needed to update this lesson.


DIFFICULTY
EASY
LINUX UNDERSTANDING
SOME
COMPUTER FAMILIARITY
LITTLE
PYTHON PROGRAMMING
SOME
ABOUT
0
MINUTES
  • How to add text to an image
  • How to automate writing text to an image in the context of image capture
  • How to save new images with unique file names

You can copy / paste the code below if you’re having issues with typos or want a shortcut. However I recommend that you follow along in the tutorial to understand what is going on!

In this tutorial I actually wrote two scripts. cookieTimestamp.py and cookieTimestampv2.py. I have included both of these scripts here.

cookieTimestamp.py


import picamera
from subprocess import call

# Our filename
fileName = "/home/pi/Desktop/cookie/newpic.jpg"

# Take a picture using our camera
with picamera.PiCamera() as camera:
    camera.resolution = (1280, 720)
    camera.capture(fileName)
    print("We have taken a picture.")

# Our timestamp's message
print("About to timestamp our picture.")
timestampMessage = "Check out this message!"
# Specify the command we want to call
timestampCommand = "/usr/bin/convert " + fileName + " -pointsize 32 \
-fill red -annotate +700+500 '" + timestampMessage + "' " + fileName
# Execute our command
call([timestampCommand], shell=True)
print("Picture has been timestamped.")

cookieTimestampv2.py


import picamera
from subprocess import call
from datetime import datetime
from time import sleep

# Our file path
filePath = "/home/pi/Desktop/cookie/timestamped_pics/"
picTotal = 5
picCount = 0

while picCount < picTotal:

    # Grab the current time
    currentTime = datetime.now()
    # Create file name for our picture
    picTime = currentTime.strftime("%Y.%m.%d-%H%M%S")
    picName = picTime + '.jpg'
    completeFilePath = filePath + picName

    # Take picture using new filepath
    with picamera.PiCamera() as camera:
        camera.resolution = (1280,720)
        camera.capture(completeFilePath)
        print("We have taken a picture.")

    # Create our stamp variable
    timestampMessage = currentTime.strftime("%Y.%m.%d - %H:%M:%S")
    # Create time stamp command to have executed
    timestampCommand = "/usr/bin/convert " + completeFilePath + " -pointsize 36 \
    -fill red -annotate +700+650 '" + timestampMessage + "' " + completeFilePath
    # Actually execute the command!
    call(&#91;timestampCommand&#93;, shell=True)
    print("We have timestamped our picture!")

    # Advance our picture counter
    picCount += 1
    sleep(3)

>


If you’re interested in a little more on this topic, check out: