Bird
Raised Fist0
Raspberry Piprogramming~20 mins

Capturing still images in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Raspberry Pi Camera Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Raspberry Pi camera capture code?
Consider the following Python code using the picamera library to capture an image. What will be the output after running this code?
Raspberry Pi
import picamera
with picamera.PiCamera() as camera:
    camera.resolution = (1024, 768)
    camera.capture('image.jpg')
print('Image captured with resolution:', camera.resolution)
ASyntaxError: invalid syntax
BImage captured with resolution: (640, 480)
CImage captured with resolution: (1024, 768)
DRuntimeError: camera not found
Attempts:
2 left
💡 Hint
Check the resolution set before capturing the image.
🧠 Conceptual
intermediate
1:30remaining
Which option correctly explains the role of 'camera.capture()' in Raspberry Pi image capture?
In Raspberry Pi Python code using the picamera library, what does the method 'camera.capture()' do?
AIt captures a still image and saves it to a file.
BIt starts the camera preview on the screen.
CIt records a video stream to a file.
DIt sets the camera resolution.
Attempts:
2 left
💡 Hint
Think about what 'capture' means in photography.
🔧 Debug
advanced
2:30remaining
What error does this Raspberry Pi camera code raise?
Analyze the following code snippet. What error will it raise when executed on a Raspberry Pi with a camera module? import picamera camera = picamera.PiCamera() camera.capture('photo.jpg') camera.close() camera.capture('photo2.jpg')
Raspberry Pi
import picamera
camera = picamera.PiCamera()
camera.capture('photo.jpg')
camera.close()
camera.capture('photo2.jpg')
ASyntaxError: invalid syntax
BFileNotFoundError: photo2.jpg not found
CNo error, both images are captured successfully
DRuntimeError: Camera has been closed
Attempts:
2 left
💡 Hint
Consider what happens after calling camera.close()
📝 Syntax
advanced
2:00remaining
Which option contains the correct syntax to capture an image with a delay using picamera?
You want to capture an image after a 5-second delay to allow the camera to adjust. Which code snippet is syntactically correct?
A
import picamera
import time
with picamera.PiCamera() as camera:
    time.sleep(5)
    camera.capture('delayed.jpg')
B
import picamera
import time
with picamera.PiCamera() as camera
    time.sleep(5)
    camera.capture('delayed.jpg')
C
import picamera
import time
camera = picamera.PiCamera()
time.sleep(5)
camera.capture('delayed.jpg')
camera.close()
D
import picamera
import time
with picamera.PiCamera() as camera:
    camera.capture('delayed.jpg')
    time.sleep(5)
Attempts:
2 left
💡 Hint
Check for missing colons and order of operations.
🚀 Application
expert
3:00remaining
How many images are saved after running this Raspberry Pi camera script?
Given the following Python script using picamera, how many image files will be saved on disk after execution? import picamera with picamera.PiCamera() as camera: camera.resolution = (640, 480) for i in range(3): camera.capture(f'image_{i}.jpg') camera.capture('final.jpg')
Raspberry Pi
import picamera
with picamera.PiCamera() as camera:
    camera.resolution = (640, 480)
    for i in range(3):
        camera.capture(f'image_{i}.jpg')
    camera.capture('final.jpg')
A3
B4
C1
D0
Attempts:
2 left
💡 Hint
Count all capture calls in the code.

Practice

(1/5)
1. What is the main purpose of the capture() method in the PiCamera library when capturing still images?
easy
A. To take a photo and save it to a file
B. To start the camera preview on the screen
C. To stop the camera and release resources
D. To adjust the camera's brightness settings

Solution

  1. Step 1: Understand the role of capture()

    The capture() method is used to take a still photo with the Raspberry Pi camera.
  2. Step 2: Identify what capture() does with the photo

    It saves the captured image to a file path you specify, storing the photo on disk.
  3. Final Answer:

    To take a photo and save it to a file -> Option A
  4. Quick Check:

    capture() saves image = C [OK]
Hint: Remember: capture() means take and save a photo [OK]
Common Mistakes:
  • Confusing capture() with start_preview()
  • Thinking capture() only shows image without saving
  • Mixing capture() with camera stop methods
2. Which of the following is the correct syntax to start the camera preview using PiCamera in Python?
easy
A. camera.startPreview()
B. camera.preview.start()
C. start.camera_preview()
D. camera.start_preview()

Solution

  1. Step 1: Recall the PiCamera method to start preview

    The correct method to start the camera preview is start_preview() called on the camera object.
  2. Step 2: Check method name and syntax

    The method is lowercase with underscore: camera.start_preview(). Other options have wrong casing or order.
  3. Final Answer:

    camera.start_preview() -> Option D
  4. Quick Check:

    Correct method name = A [OK]
Hint: Use underscores and lowercase for PiCamera methods [OK]
Common Mistakes:
  • Using camelCase instead of snake_case
  • Reversing method and object order
  • Adding extra dots or missing underscores
3. What will be the output of this code snippet when run on a Raspberry Pi with a camera connected?
from picamera import PiCamera
from time import sleep
camera = PiCamera()
camera.start_preview()
sleep(2)
camera.capture('/home/pi/image.jpg')
camera.stop_preview()
medium
A. The program throws an error because sleep is too short
B. The camera preview shows for 2 seconds, then saves image.jpg
C. No image is saved because stop_preview() is called too early
D. The preview never starts because start_preview() is missing

Solution

  1. Step 1: Analyze the preview and sleep timing

    The preview starts, then the program waits 2 seconds allowing the camera to adjust.
  2. Step 2: Capture and save the image after preview

    After waiting, the image is captured and saved to '/home/pi/image.jpg', then preview stops.
  3. Final Answer:

    The camera preview shows for 2 seconds, then saves image.jpg -> Option B
  4. Quick Check:

    Preview + sleep + capture = A [OK]
Hint: Preview then sleep before capture ensures good image [OK]
Common Mistakes:
  • Assuming sleep is too short causes error
  • Thinking stop_preview() prevents saving
  • Believing start_preview() is missing
4. Identify the error in this code snippet that tries to capture an image:
from picamera import PiCamera
camera = PiCamera()
camera.capture('photo.jpg')
camera.start_preview()
medium
A. The preview should start before capturing the image
B. The capture method requires a full file path, not just filename
C. PiCamera must be imported after starting preview
D. The camera object is not created correctly

Solution

  1. Step 1: Check the order of preview and capture

    The preview should start before capturing to allow camera to adjust and focus.
  2. Step 2: Identify the mistake in code order

    Here, capture is called before start_preview(), which can cause poor image quality or errors.
  3. Final Answer:

    The preview should start before capturing the image -> Option A
  4. Quick Check:

    Preview before capture = B [OK]
Hint: Always start preview before capture for proper image [OK]
Common Mistakes:
  • Calling capture before preview starts
  • Assuming filename alone is invalid path
  • Thinking import order affects capture
5. You want to capture multiple images with unique filenames using PiCamera in a loop. Which code snippet correctly saves images as 'image1.jpg', 'image2.jpg', and 'image3.jpg'?
hard
A. for i in range(1,4): camera.capture('image{}.jpg'.format(i+1))
B. for i in range(3): camera.capture('image{i+1}.jpg')
C. for i in range(1,4): camera.capture(f'image{i}.jpg')
D. for i in range(1,4): camera.capture('image' + i + '.jpg')

Solution

  1. Step 1: Understand how to create unique filenames in a loop

    Using an f-string with f'image{i}.jpg' correctly inserts the loop variable into the filename.
  2. Step 2: Check each option for correct syntax and output

    for i in range(1,4): camera.capture(f'image{i}.jpg') uses f-string with range(1,4) producing 'image1.jpg', 'image2.jpg', 'image3.jpg'. for i in range(3): camera.capture('image{i+1}.jpg') uses single quotes without f-string, so filename is literal. for i in range(1,4): camera.capture('image' + i + '.jpg') tries to add string and int without conversion, causing error. for i in range(1,4): camera.capture('image{}.jpg'.format(i+1)) adds 1 to i incorrectly, producing 'image2.jpg', 'image3.jpg', 'image4.jpg'.
  3. Final Answer:

    for i in range(1,4): camera.capture(f'image{i}.jpg') -> Option C
  4. Quick Check:

    f-string with correct range = D [OK]
Hint: Use f-strings and range starting at 1 for numbered files [OK]
Common Mistakes:
  • Using string concatenation without converting int to str
  • Forgetting f-string syntax for variable insertion
  • Incorrect range causing wrong filenames
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard