User avatar
65mustang
Posts: 8
Joined: Sat Jul 27, 2013 3:23 pm

RASPISTILL on Python

Sun Jul 28, 2013 4:55 pm

Hello,

So i got the Pi bug! had mine few days ago and next day I added the PiFace digital board and the camera board. At the same time, I was exposed to Python programming. I manage to study a bit about the language and made some small scripts to test the input buttons on the PiFace board. I then upgrade the script so it will trigger the camera by running an OS command line.. However!(here I go) I noticed that even though I took X number of pictures.. only the last one(last picture) will be saved and will appear on the OS. I think it's because of the "while" loop.. it doesn't close until I terminate the program? Help!

Anybody can give me some hints/input? I wanted to build a small mobile camera out of my Pi.

My script below: (sorry.. quite Elementary scripting)

from time import sleep
import datetime,os,cgitb,sys,datetime, piface.pfio as pfio
cgitb.enable()
pfio.init()
sys.path.insert(0, "usr/bin/espeak")
sys.path.insert(0, "usr/bin/raspistill")
utc_datetime = datetime.datetime.utcnow()
s=utc_datetime.strftime("%Y-%m-%d-%H%M%SZ")
filename = 'IMG_' + s + '.jpg'

def say(something):
os.system('sudo espeak "{0}"'.format(something))

def still():
os.system('raspistill -o ' + filename)

while(True):
button_state1 = pfio.digital_read(0)

if button_state1 == 1:
pfio.digital_write (0,1)
say("Taking picture!")
still()

elif button_state1 == 0:
pfio.digital_write (0,0)

sleep(0.2)
"The thrill of life is never trumped by the regret of not trying."
"My style is a little bit unorthodox. But dont worry, I always get it done."
"If it doesn't fit, force it. If it breaks, it needed to be replaced anyway."

jakebpg
Posts: 36
Joined: Tue Sep 04, 2012 9:36 pm

Re: RASPISTILL on Python

Sun Jul 28, 2013 10:50 pm

It appears that you are over writing the file over and over again in your while loop.

Try copying these lines into your while loop just before you write the file to disk.

s=utc_datetime.strftime("%Y-%m-%d-%H%M%SZ")
filename = 'IMG_' + s + '.jpg'

By doing this it will give each file a unique filename by the time stamp in the line starting with s=

Let me know if this helps!

User avatar
65mustang
Posts: 8
Joined: Sat Jul 27, 2013 3:23 pm

Re: RASPISTILL on Python

Sun Jul 28, 2013 11:58 pm

HI, Thanks... Tried but no luck. I even try to put is inside the still() function but still unfruitful. Its still using the same filename over and over again.
"The thrill of life is never trumped by the regret of not trying."
"My style is a little bit unorthodox. But dont worry, I always get it done."
"If it doesn't fit, force it. If it breaks, it needed to be replaced anyway."

texy
Forum Moderator
Forum Moderator
Posts: 5162
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: RASPISTILL on Python

Mon Jul 29, 2013 6:48 am

The problem is that filename is only defined outside the loop (in your original listing). It needs to be updated prior taking the picture, otherwise it will always save the latest image using the same filename and hence only 1 jpg is saved (As jakebpg has already pointed out).
List your complete program as it is now.............
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

User avatar
65mustang
Posts: 8
Joined: Sat Jul 27, 2013 3:23 pm

Re: RASPISTILL on Python

Mon Jul 29, 2013 7:17 am

texy wrote:The problem is that filename is only defined outside the loop (in your original listing). It needs to be updated prior taking the picture, otherwise it will always save the latest image using the same filename and hence only 1 jpg is saved (As jakebpg has already pointed out).
List your complete program as it is now.............
Texy
Hi Texy, i tried putting the definition of the filename inside the While loop but seemed that its still uses the original data.

Test 1: put it inside the while loop that when the Input button 1 is pressed, then assigned the date to variable filename
from time import sleep
import datetime,os,cgitb,sys,datetime, piface.pfio as pfio
cgitb.enable()
pfio.init()
sys.path.insert(0, "usr/bin/espeak")
sys.path.insert(0, "usr/bin/raspistill")

def say(something):
os.system('sudo espeak "{0}"'.format(something))

def still():
os.system('raspistill -o ' + filename)

while(True):
button_state1 = pfio.digital_read(0)

if button_state1 == 1:
pfio.digital_write (0,1)
utc_datetime = datetime.datetime.utcnow()
s=utc_datetime.strftime("%Y-%m-%d-%H%M%SZ")
filename = 'IMG_' + s + '.jpg'

say("Taking picture!")
still()

elif button_state1 == 0:
pfio.digital_write (0,0)

sleep(0.2)


Test 2: Was thinking if the still() was called then that will be the only time the filename value will be assigned.
from time import sleep
import datetime,os,cgitb,sys,datetime, piface.pfio as pfio
cgitb.enable()
pfio.init()
sys.path.insert(0, "usr/bin/espeak")
sys.path.insert(0, "usr/bin/raspistill")
utc_datetime = datetime.datetime.utcnow()

def say(something):
os.system('sudo espeak "{0}"'.format(something))

def still():
utc_datetime = datetime.datetime.utcnow()
s=utc_datetime.strftime("%Y-%m-%d-%H%M%SZ")
filename = 'IMG_' + s + '.jpg'

os.system('raspistill -o ' + filename)

while(True):
button_state1 = pfio.digital_read(0)

if button_state1 == 1:
pfio.digital_write (0,1)
say("Taking picture!")
still()

elif button_state1 == 0:
pfio.digital_write (0,0)

sleep(0.2)


at the back of my head, i am thinking of putting another while loop inside my existing while loop?? hmmn....
"The thrill of life is never trumped by the regret of not trying."
"My style is a little bit unorthodox. But dont worry, I always get it done."
"If it doesn't fit, force it. If it breaks, it needed to be replaced anyway."

User avatar
65mustang
Posts: 8
Joined: Sat Jul 27, 2013 3:23 pm

Re: RASPISTILL on Python

Mon Jul 29, 2013 7:28 am

hmmn... tried it here on my Windows platform using a simple While statement and yes! it changes the value of the filename.

from time import sleep
import datetime,os,cgitb,sys,

while(True):
utc_datetime = datetime.datetime.utcnow()
s=utc_datetime.strftime("%Y-%m-%d-%H%M%SZ")
filename = 'IMG_' + s + '.jpg'
print filename

sleep(3)
"The thrill of life is never trumped by the regret of not trying."
"My style is a little bit unorthodox. But dont worry, I always get it done."
"If it doesn't fit, force it. If it breaks, it needed to be replaced anyway."

User avatar
65mustang
Posts: 8
Joined: Sat Jul 27, 2013 3:23 pm

Re: RASPISTILL on Python

Mon Jul 29, 2013 11:50 am

PROBLEM SOLVED! Thanks Texy and Jakebpg


from time import sleep
import datetime,os,cgitb,sys, piface.pfio as pfio
cgitb.enable()
pfio.init()
sys.path.insert(0, "usr/bin/espeak")
sys.path.insert(0, "usr/bin/raspistill")

def say(something):
os.system('sudo espeak "{0}"'.format(something))

def still():
print filename
os.system('raspistill -o ' + filename + ' -t 0')

while(True):
button_state1 = pfio.digital_read(0)
utc_datetime = datetime.datetime.utcnow()
s=utc_datetime.strftime("%Y-%m-%d-%H%M%SZ")
filename = 'IMG_' + s + '.jpg'

if button_state1 == 1:
pfio.digital_write (0,1)
say("Taking picture!")
still()

elif button_state1 == 0:
pfio.digital_write (0,0)

sleep(1)

"The thrill of life is never trumped by the regret of not trying."
"My style is a little bit unorthodox. But dont worry, I always get it done."
"If it doesn't fit, force it. If it breaks, it needed to be replaced anyway."

Return to “Python”