1

I have started coding a program in Python 3 for the Raspberry Pi Camera, here is what my code looks like:

import picamera, time

def Picture_Name_Settings():
    pic_name = input("To name the image input N\nTo use automatic naming input A ")
    pic_name = pic_name.upper()
    if pic_name == "N":
        user_name = input("Please enter the name of what you want your image to be called ")

    if pic_name == "A":
        current_time = (time.strftime("%H:%M:%S"))
        current_date = (time.strftime("%d:%m:%Y"))
        automatic_name = ("Image taken at", current_time, "on the", current_date, "jpeg")
        print(automatic_name)
        camera = picamera.PiCamera()
        camera.capture(automatic_name)
        print("Took the picture")

Picture_Name_Settings()

And the error I get when it is run and after inputting "a" is:

'Format must be specified when output has no filename'

picamera.exc.PiCameraValueError: Format must be specified when output has no filename

Why am I getting this error? And why can I not put a variable containing the filename and type into the camera.capture brackets?

  • Although I can not imagine why it would be an issue maybe you need to give your file name an extension, instead of "jpeg" try ".jpg" (or ".jpeg") – rob Apr 7 '15 at 10:37
  • I tried changing the "jpeg" with ".jpeg" and ".jpg", both give exactly the same error that was having before but thanks for the help – Darth Vader Apr 7 '15 at 10:46
3

The short version is: you're passing the capture method a tuple instead of a string. You probably want something like the following for the name:

automatic_name = "".join(("Image taken at", current_time, "on the", current_date, ".jpg"))

The long version is:

The capture method follows the python idiom of accepting a filename (as a string) or a file-like object. In the following line you are constructing a tuple, not a string:

automatic_name = ("Image taken at", current_time, "on the", current_date, "jpeg")

So, back to the capture method. Why is it throwing such a confusing error?

When you pass the method the tuple it assumes "it's not a string, so I'll try and use it as a file-like object". What is a file like object? It's any object with a write method. The tuple doesn't have a write method but that doesn't matter yet because first the capture method needs to know what format (JPEG, PNG, etc) it should use. To figure this out it looks at the format parameter, which you haven't passed so it defaults to None meaning "figure it out from the filename". So the capture method tries to look at the name attribute of the supposed file like object... Unfortunately the tuple doesn't have one of those either so at this point the capture method blows up and complains that a format must be specified.

This is relatively typical of programming: the error is fairly early on but the actual exception that gets raised is somewhere further down the line and it takes a certain amount of "under the hood" knowledge to track back to the actual issue.

It's rather compounded in this case due to the dynamic typing in python although I could improve the checks at the start of that method to test for a write method on the file-like object up front (which would make the error message a little more useful)

1

Here is a similar project I wrote a while ago. Hopefully this can help you in some way or another. It is not using PiCamera like you are but a more basic approach that I came up with at the time.

CamModPy

See example below. But this will only work if you have the Camera.py file in your running folder.

import Camera

if __name__ == '__main__':
    cam = Camera.image().On();
    cam.clean();
    cam.setlocation("test");
    cam.capture("GeneralTest.jpg");
    cam.clean();
  • Could you direct me to the code that actually determines the name and file type of the picture? And what version of python was that coded in? Thanks for the help and that code looks neater and more efficient than most stuff I code – Darth Vader Apr 8 '15 at 8:57
  • I added the code above. You can look at the tester.py file for more examples of how to use it. – Kwekuq Apr 10 '15 at 1:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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