0

I would be very grateful for some guidance on the following script. You will see that I have very little experience (3 days!).

Using the pantilthat servo control, I am taking a image (jpg) at various camera positions. This is in a continuous loop that will be run during daylight hours.

I am trying to save the images like so, 1.jpg, 2.jpg, 3.jpg..... 1.jpg, 2.jpg, 3.jpg.... etc etc

The code works, but I just cant figure out the file naming part. Thank you for looking and any improvents gratefully received.


#!/usr/bin/env python
import pantilthat
import time
import picamera
x = 0
while x < 9:
        print "Take Photo"
        with picamera.PiCamera() as camera:
                camera.resolution = (1024, 768)
                camera.start_preview()
                camera.annotate_text = 'Picture Taken with Raspberry camera'
                time.sleep(2)
                for filename in camera.record_sequence(
                'image%02d.jpg' % i for i in range(1)):
                        print('Saving to %s' % filename)
                camera.stop_preview()
                print('Done')
        x += 1
        if x == 1:
                print("1")
                pantilthat.pan(30)
                pantilthat.tilt(-10)
                time.sleep(1)
        elif x == 2:
                print("2")
                pantilthat.pan(20)
                pantilthat.tilt(0)
                time.sleep(1)
         elif x == 3:
                pantilthat.pan(10)
                pantilthat.tilt(10)
                print("3")
                time.sleep(1)
        elif x == 4:
                pantilthat.pan(0)
                pantilthat.tilt(-10)
                print("4")
                time.sleep(1)
        elif x == 5:
                pantilthat.pan(-10)
                pantilthat.tilt(10)
                print("5")
                time.sleep(1)
        elif x == 6:
                pantilthat.pan(-20)
                pantilthat.tilt(-20)
                print("6")
                time.sleep(1)
       elif x == 7:
                pantilthat.pan(-30)
                pantilthat.tilt(20)
                print("7")
                time.sleep(1)
                x=0
        else:
                pass
  • According to the docs record_sequence records a sequence of video clips! picamera.readthedocs.io/en/release-1.10/… Don't you just want to capture one image each time the camera moves? – CoderMike Feb 3 at 17:42
  • 1
    you explained what filenames you want ..... you did not explain what filenames you get when you run the program – jsotola Feb 3 at 18:33
  • 1
    looks like your camera gets positioned after you take a picture .... is that how you want the program to behave? – jsotola Feb 3 at 19:20
2

How about something like the following.

I'm using a list to store your pan and tilt values and capturing 1 image on each loop. Simply concatenating the string value of i onto 'image' followed by .'jpg' to create the image name.

#!/usr/bin/env python3
import pantilthat,time,picamera

panTilt = [[ 30, -10],
           [ 20,   0],
           [ 10,  10],
           [  0, -10],
           [-10,  10],
           [-20, -20],
           [-30,  20],
           [  0,   0]]

camera=picamera.PiCamera()
camera.resolution = (1024, 768)
camera.annotate_text = 'Picture Taken with Raspberry camera'

for i,(pan,tilt) in enumerate(panTilt):
    print("Take Photo")
    camera.capture('image'+str(i)+'.jpg')
    print(i,pan,tilt)
    pantilthat.pan(pan)
    pantilthat.tilt(tilt)
    time.sleep(1)

camera.close()
  • That is a good way to significantly simplify the OP's code. To make it (imho) slightly more pythonic you could turn that while loop to: for i, (pan, tilt) in enumerate(panTilt): and use the i for counting up the filename and pan and tilt instead of panTilt[x][0]. ` – Ghanima Feb 3 at 18:31
  • @CoderMike What a neat method you have provided with the table. I don't know why, but when I run your code, I get an error after image 6. I inserted a sleep period (60 secs) thinking it might be the camera not keeping up with instructions, but the error persisted. Do I need to keep the interpreter 'alive' ? This is the error message ; – Rixster Feb 3 at 20:52
  • Exception in thread Thread-13 (most likely raised during interpreter shutdown): Traceback (most recent call last): File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner File "/usr/lib/python2.7/threading.py", line 1071, in run File "/usr/lib/python2.7/threading.py", line 614, in wait File "/usr/lib/python2.7/threading.py", line 364, in wait <type 'exceptions.ValueError'>: list.remove(x): x not in list – Rixster Feb 3 at 20:53
  • I also noticed that the picam was 'on' permantly when I ran the code (as seen by the constant red LED). Would it not best to turn the camera off between images so as not to 'wear it out'? @Ghanima. Thanks for your suggestions to make it more 'pythonic', but I am struggling to make it work at all ! (Incidentally, the camera will be used to monitor an airfield) thanks again – Rixster Feb 3 at 20:55
  • 'image%s.jpg'%i :P – somebody Feb 3 at 22:07

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.