Crowpi2, how can I take a photograph?

How do I utilize the built-in camera to take photographs?

Tagged:

Comments

  • edited June 2022

    You can use the fswebcam app to capture photos with the built-in webcam with a resolution upto VGA (640x480). For detailed usage, consult the Man Page (man fswebcam). Or the Raspberry Pi official documentation.

    A quick example of taking a photo:

    1) Open a new Terminal window (Ctrl+Alt+t).

    2) Type the following command:

    fswebcam -r 640x480 Photo1.jpg

    3) Post in front of the camera, when ready to take a photo, press Enter.

    4) The new photo should be saved as Photo1.jpg in your home directory.

    For additional explanation, you can view this short Youtube video.

  • Problem:

    • On the quick example above in step 2, the output file 'Photo1.jpg' will be over written without prompting each time when a picture is taken.

    Solution:

    You can write a Bash script which takes a picture with the webcam and save it with a filename based on the current time. The example script below saves the images in the /home/pi/webcam directory, so create the webcam subdirectory first with:

    mkdir /home/pi/webcam
    

    To create a script named webcam.sh, open up your editor of choice and write (or copy & paste) the following example code:

    #!/bin/bash
    
    DATE=$(date +"%Y-%m-%d_%H%M%S")
    
    fswebcam -r 640x480 --no-banner /home/pi/webcam/$DATE.jpg
    

    This script will take a picture and name the image file with a timestamp. Say we saved the script as webcam.sh, we would first make the file executable:

    chmod +x webcam.sh
    

    Then run with:

    ./webcam.sh
    

    Which would run the commands in the file and give the usual output:

    --- Opening /dev/video0...
    Trying source module v4l2...
    /dev/video0 opened.
    No input was specified, using the first.
    --- Capturing frame...
    Corrupt JPEG data: 2 extraneous bytes before marker 0xd6
    Captured frame in 0.00 seconds.
    --- Processing captured image...
    Disabling banner.
    Writing JPEG image to '/home/pi/webcam/2022-06-21_143826.jpg'.
    


Sign In or Register to comment.