CY83
CY83

Reputation: 85

OpenCV takes black picture

So I found this code, that's supposed to take a picture, on the internet and modified it a bit so it doesn't give me a bunch of errors:

import cv2

videoCaptureObject = cv2.VideoCapture(1,cv2.CAP_DSHOW)
result = True
while(result):
    ret,frame = videoCaptureObject.read()
    cv2.imwrite("NewPicture.jpg",frame)
    result = False
videoCaptureObject.release()
cv2.destroyAllWindows()

But when I take a picture, the picture is just black.

How do I fix this?

Upvotes: 0

Views: 754

Answers (1)

kostas_math
kostas_math

Reputation: 80

You could try to "run" your camera and take a picture (of the frame) whenever you want by pressing a specific key-button from your keyboard.

import cv2

# Video captute
# 0 or 1 based on the type and the number of your cameras
cap = cv2.VideoCapture(1)

while True:
    _, frame = cap.read()      # We don't want ret in this
    cv2.imshow("Frame", frame) # Show the current frame

    key = cv2.waitKey(1)
    if key==27: # If you press Esc then the frame window will close (and the program also)
        break
    elif key==ord('p'): # If you press p/P key on your keyboard
        cv2.imwrite("path.../pic.jpg", frame) # Save current frame as picture with name pic.jpg

cap.release()
cv2.destroyAllWindows()

Upvotes: 1

Related Questions