0

I use the following code to capture an image from PiCamera:

from picamera import PiCamera
from time import sleep

camera = PiCamera()
camera.start_preview()
sleep(5)
camera.capture('/home/pi/Desktop/image.jpg')
camera.stop_preview()

When I run the code, an image file is created on desktop but its size is 0KB. What am I doing wrong?

3
  • Your code works on my Pi3. What does 'ls ~/Desktop/image.jpg -l' show ?
    – CoderMike
    Oct 22, 2017 at 10:44
  • Are you able to capture images successfully using the raspistill command?
    – larsks
    Oct 22, 2017 at 11:52
  • Yes everything is OK with raspistill for capture the image. But with python code the file has no information.(0Byte)
    – Bab
    Oct 23, 2017 at 6:39

1 Answer 1

1

You haven't created an object for the captured information to go to. Generally speaking, I think it's easier for beginners to learn the file method (as opposed to streams). The code below is an example taken from readthedocs.io, and should help you get started.

from time import sleep from picamera import PiCamera

# Explicitly open a new file called my_image.jpg
my_file = open('my_image.jpg', 'wb')

camera = PiCamera()
camera.start_preview()
sleep(2)
camera.capture(my_file)

# At this point my_file.flush() has been called, but the file has
# not yet been closed
my_file.close()
1
  • I cpied the following code in my Cam0.py : / from time import sleep from picamera import PiCamera# Explicitly open a new file called my_image.jpg my_file = open('my_image.jpg', 'wb') camera = PiCamera() camera.start_preview() sleep(2) camera.capture(my_file) # At this point my_file.flush() has been called, but the file has # not yet been closed my_file.close() / after run the file is empty (Image file '/home/pi/Desktop/Babak/my_image.jpg' contains no data) !!!
    – Bab
    Oct 24, 2017 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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