[Python] Trouble when accessing camera (Arducam) on Raspberry PI 4

Discussão em 'Python' iniciado por Stack, Maio 6, 2025.

  1. Stack

    Stack Membro Participativo

    Question: I'm trying to capture an image using OpenCV and save it with both OpenCV and PIL on my Raspberry Pi 4. Here's my Python code:

    import cv2
    from PIL import Image

    print("1")
    cap = cv2.VideoCapture(0)
    for i in range(100):
    ret, frame = cap.read()

    if not ret:
    print("Failed to grab frame")
    break

    if i == 99:
    print("3")
    cv2.imshow("Captured Image", frame)
    cv2.imwrite("captured_image.jpg", frame)
    frame = cv2.resize(frame, (160, 90))
    pil_image = Image.fromarray(frame)
    pil_image.save("captured_image_pil.jpg")
    print("4")


    Problem:

    1. cv2.imwrite() does not actually save the image — the file doesn't show up or is empty.
    2. The image saved by PIL is completely black.
    3. When I run libcamera-hello, the camera works fine and shows output.

    What I tried:

    • Verified camera works using libcamera-hello.
    • Checked if the frames are being captured (they seem to be).

    My question:

    Why doesn't OpenCV save the image, and why is the PIL image black, even though the camera works with libcamera-hello? How can I fix this? My camera model is Arducam IMX 708

    Continue reading...

Compartilhe esta Página