User avatar
TheNerdyTeacher
Posts: 5
Joined: Tue Feb 02, 2016 1:44 pm
Location: Detroit, MI
Contact: Website

Timestamp multiple photos.

Sun Aug 07, 2016 4:22 pm

I've been putting together a code to create a photo booth for a school events, but I have run into one small problem that I cannot overcome. Each new photo created has the same name and overwrites the old one when it gets uploaded to Dropbox. Below is the code.

Code: Select all

from os import system
import picamera
import time
from gpiozero import Button


camera = picamera.PiCamera()
button = Button(18)

camera.start_preview()


while True:
    try:
        button.wait_for_press()
        camera.annotate_text='5'
        time.sleep(1)
        camera.annotate_text='4'
        time.sleep(1)
        camera.annotate_text='3'
        time.sleep(1)
        camera.annotate_text='2'
        time.sleep(1)
        camera.annotate_text='1'
        time.sleep(1)
        camera.annotate_text=' '
        camera.capture("aaa.jpg")
        camera.annotate_text='3'
        time.sleep(1)
        camera.annotate_text='2'
        time.sleep(1)
        camera.annotate_text='1'
        time.sleep(1)
        camera.annotate_text=' '
        camera.capture("bbb.jpg")
        camera.annotate_text='3'
        time.sleep(1)
        camera.annotate_text='2'
        time.sleep(1)
        camera.annotate_text='1'
        time.sleep(1)
        camera.annotate_text=' '
        camera.capture("ccc.jpg")
        camera.stop_preview()
        camera.close

        print "Cropping photo1"
        system("convert aaa.jpg -resize x400 -gravity center -crop 400x400+0+0 =repage aaa_crop.jpg")
        print "Croppinng photo2"
        system("convert bbb.jpg -resize x400 -gravity center -crop 400x400+0+0 =repage bbb_crop.jpg")
        print "Croppinng photo3"
        system("convert ccc.jpg -resize x400 -gravity center -crop 400x400+0+0 =repage ccc_crop.jpg")
        print "resizing photo1"
        system("convert aaa_crop.jpg -extent 1200x1800-100-100 photo_strip.jpg")
        print "top right"
        system("composite -compose atop -geometry +700+100 aaa_crop.jpg photo_strip.jpg photo_strip.jpg")
        print "middle left"
        system("composite -compose atop -geometry +100+700 bbb_crop.jpg photo_strip.jpg photo_strip.jpg")
        print "middle right"
        system("composite -compose atop -geometry +700+700 bbb_crop.jpg photo_strip.jpg photo_strip.jpg")
        print "bottom left"
        system("composite -compose atop -geometry +100+1300 ccc_crop.jpg photo_strip.jpg photo_strip.jpg")
        print "bottom right"
        system("composite -compose atop -geometry +700+1300 ccc_crop.jpg photo_strip.jpg photo_strip.jpg")
        print "Uploading to Dropbox"
        system ("dropbox_uploader upload photo_strip.jpg 'Nerdy Booth'/")
                
    except KeyboardInterrupt:
        camera.stop_preview()
        break
    camera.start_preview()
    
I'm not sure how to get the file to change names each time. Maybe my code is overly complex and the problem is simpler than I realize. Any help would be appreciated.

Thanks!
Nicholas Provenzano
ISTE & MACUL Teacher of the Year
Google Certified Innovator
OneNerdyTeacher@gmail.com
@TheNerdyTeacher
TheNerdyTeacher.com

User avatar
DougieLawson
Posts: 40138
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: HOW TO: Timestamp multiple photos.

Sun Aug 07, 2016 9:02 pm

Code: Select all

        camera.capture("aaa.jpg")
Why not change those camera.capture lines to write a unique filename at that point, then pass the filename variable to the system calls that convert them, etc.

It's very easy to use current date/time as a filename.

Code: Select all

#!/usr/bin/python
import time, datetime, picamera
camera = picamera.PiCamera()
tstamp = datetime.datetime.now()
tformat = "%a-%d-%b.%H.%M.%S" # ddd-dd-mmm-hh.mm.ss.jpg
filename = tstamp.strftime(tformat)
camera.capture(filename+'.jpg')
Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Return to “Advanced users”