How do I take a picture instantly without showing frame when opening program?

As soon as the file is opened it will take a picture from the webcam and then instantly close.

what have you try?

You can open videocapture and save image without using imshow

    import cv2

cam = cv2.VideoCapture(0)


img_counter = 0

while True:
    if not ret:
        print("failed to grab frame")
    cv2.imshow("test", frame)

    k = cv2.waitKey(1)
    if k%256 == 27:
        # ESC pressed
        print("Escape hit, closing...")
    elif k%256 == 32:
        # SPACE pressed
        img_name = "opencv_frame_{}.png".format(img_counter)
        cv2.imwrite(img_name, frame)
        print("{} written!".format(img_name))
        img_counter += 1



I have this code so far can you give me an example on how to do that?

can you figure out what your code does, i.e. what all the individual lines mean and do?

I know what each line does but do you want to know? is that what you are asking?

“I don’t want to do X, yet I’m explicitly doing X in my code, which I undestand. What is wrong?”

1 Like

to be blunt: we will not be writing your code for you. you are expected to be somewhat decent at basic programming. that seems to be your issue here. you have demonstrated absolute laziness. you haven’t even read the code you posted. you’ve taken zero care and consideration of other people’s time.

Hi @Plasmatyxs

The code you posted is a pretty standar way to open the webcam. You are in the right path. There is something important missing: ret, right after while True: I believe you mean something like ret, frame =

There is a frame. after break that doesn’t make sense to me. It’s is important to post a runnable code when asking for help to debug it.

I suggest to edit the code and also try removing the if not ret: break clause and see what happens.