gius
Posts: 8
Joined: Thu Jun 07, 2018 10:04 pm

raspistill filename taking pictures pressing a button

Mon Jun 18, 2018 11:06 pm

Hi all,

I have rpi zero with camera module. I would like ro take pictures by presing a switch.
I copied a python script for this purpose. I added another switch to exit the script..
The script uses pin 18 to take a photo and pin 17 to exit the script. The script takes photos using raspistill.
Here is the script:

import RPi.GPIO as GPIO
from time import sleep
import os
import sys
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)

input = 1

while input
input_state = GPIO.input(18)
if input_state == False:
os.system(" sudo raspistill -q 100 -ev 2 -n -t 300 -o imm%04d.jpg")
sleep (0.05)
input = GPIO.input(17)

GPIO.cleanup()

sys.exit(0)


It works but it saves only one filename.jpg. I think that is because at each shot raspistill counts the %04d number from the beginning (i.e. imm0001.jpg).
I read raspistill documentation but I could not find any suggestions. It is not a timelapse but I take pictures at my own pace pressing a switch.

Could someone suggest a way of saving the images with progressive numbering at each new shot using raspistill?

Thank you,

Giuseppe

User avatar
HermannSW
Posts: 1239
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

Re: raspistill filename taking pictures pressing a button

Mon Jun 18, 2018 11:54 pm

Build a formatted command string and pass that to os.system():

Code: Select all

>>> for i in range(1, 9):
...      print 'sudo raspistill -q 100 -ev 2 -n -t 300 -o imm{:04d}.jpg'.format(i)
... 
sudo raspistill -q 100 -ev 2 -n -t 300 -o imm0001.jpg
sudo raspistill -q 100 -ev 2 -n -t 300 -o imm0002.jpg
sudo raspistill -q 100 -ev 2 -n -t 300 -o imm0003.jpg
sudo raspistill -q 100 -ev 2 -n -t 300 -o imm0004.jpg
sudo raspistill -q 100 -ev 2 -n -t 300 -o imm0005.jpg
sudo raspistill -q 100 -ev 2 -n -t 300 -o imm0006.jpg
sudo raspistill -q 100 -ev 2 -n -t 300 -o imm0007.jpg
sudo raspistill -q 100 -ev 2 -n -t 300 -o imm0008.jpg
>>> 
bookmark list: https://stamm-wilbrandt.de/en/Raspberry_camera.html

https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://github.com/Hermann-SW/fork-raspiraw
https://github.com/Hermann-SW/userland
https://twitter.com/HermannSW

gius
Posts: 8
Joined: Thu Jun 07, 2018 10:04 pm

Re: raspistill filename taking pictures pressing a button

Tue Jun 19, 2018 11:57 am

Thank you very much HermannSW.

I am totally new to rpi, python, etc.
Do I have to write the os.system() this way:

os.system(for i in range(1, 9):
print 'sudo raspistill -q 100 -ev 2 -n -t 300 -o imm{:04d}.jpg'.format(i))

I.e. :04d or %04d? May I change number 9 in the 'for i loop' to whatever less than 9999?

I would like to use rpi+camera module like a battery powered point and shot camera (of course without any keyboard or monitor) to take still pictures.
I tried also a python script using 'raspistill -k -t 0'. It waits for pressing enter key taking pictures forever (i.e. untill you stop pressing 'x') saving pictures filenames with progressive numbers:

import RPi.GPIO as GPIO
from time import sleep
import os
import sys
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)

input = 1
os.system(" sudo raspistill -q 100 -ev 2 -n -t 0 -k -o imm%04d.jpg")
while input:
input_state = GPIO.input(18)
if input_state == False:
os.system(" sudo echo") # I just wanted to send raspistill an enter keypress but it does not work
sleep (0.05)
input = GPIO.input(17)
if input == False:
os.system(sudo kill -9 $(pidof raspistill)
GPIO.cleanup()

sys.exit(0)

thank you for any help and suggestions.

Giuseppe

User avatar
HermannSW
Posts: 1239
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

Re: raspistill filename taking pictures pressing a button

Tue Jun 19, 2018 6:56 pm

Do I have to write the os.system() this way:

os.system(for i in range(1, 9):
print 'sudo raspistill -q 100 -ev 2 -n -t 300 -o imm{:04d}.jpg'.format(i))
No, you have to store the formatted string in a variable (str) and then do os.system(str).
I prepended "echo" command to see what goes on:

Code: Select all

>>> import os
>>> for i in range(1, 9):
...     str = 'echo sudo raspistill -q 100 -ev 2 -n -t 300 -o imm{:04d}.jpg'.format(i)
...     dummy = os.system(str)
... 
sudo raspistill -q 100 -ev 2 -n -t 300 -o imm0001.jpg
sudo raspistill -q 100 -ev 2 -n -t 300 -o imm0002.jpg
sudo raspistill -q 100 -ev 2 -n -t 300 -o imm0003.jpg
sudo raspistill -q 100 -ev 2 -n -t 300 -o imm0004.jpg
sudo raspistill -q 100 -ev 2 -n -t 300 -o imm0005.jpg
sudo raspistill -q 100 -ev 2 -n -t 300 -o imm0006.jpg
sudo raspistill -q 100 -ev 2 -n -t 300 -o imm0007.jpg
sudo raspistill -q 100 -ev 2 -n -t 300 -o imm0008.jpg
>>> 

You need to put the for loop around

Code: Select all

input = 1
...
GPIO.cleanup()
I don't use Python.
Only MicroPython sometimes as debug shell for ESP32s after soldering or when testing motor controllers+motors:
https://www.esp32.com/viewtopic.php?t=3476
Image
bookmark list: https://stamm-wilbrandt.de/en/Raspberry_camera.html

https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://github.com/Hermann-SW/fork-raspiraw
https://github.com/Hermann-SW/userland
https://twitter.com/HermannSW

gius
Posts: 8
Joined: Thu Jun 07, 2018 10:04 pm

Re: raspistill filename taking pictures pressing a button

Wed Jun 20, 2018 10:55 am

Thank you very much for the code and interesting info.

I put the code with the for loop and it takes 9 foto (saving imm0001, 2...) as I press the button once.
I need to take one photo as I press the button, then I take another photo... and I will have the photos saved as imm0001, 2, .....

I put your code without the for loop as follows (BTW, I omitted 'dummy' which I do not know what is: as I said I am totally new in coding in any code, just copying here and there):

.....
while input:
input_state = GPIO.input(17)
if input_state == False:
str = 'sudo raspistill -q 100 -ev 2 -n -t 300 -o imm{:04d}.jpg'.format(i)
os.system(str)
sleep (0.05)
i = i+1
input = GPIO.input(18)
..........

And it works fine.

Thank you again for your help in solving my issue and learning some coding.

Best regards,

Giuseppe

Return to “Camera board”