Search code examples
pythonmacoscamerawebcamblurry

Issue with webcam - blurry background (MacOS)


I am running the below simple Python code (Python 3.11.7 with OpenCV 4.8.0) on my Mac (M2) so I can capture and display images from my camera Logitech C925e.

    # import the opencv library 
    import cv2   
    # define a video capture object 
    vid = cv2.VideoCapture(0) 
    while(True): 
        # Capture the video frame by frame 
        ret, frame = vid.read() 
        # Display the resulting frame 
        cv2.imshow('frame', frame) 
        # the 'q' button is set as quitting button
        if cv2.waitKey(1) & 0xFF == ord('q'): 
            break
    # After the loop release the cap object 
    vid.release() 
    # Destroy all the windows 
    cv2.destroyAllWindows() 

The issue is that the images show a blurry background.

Using the Logitech app (Logitune.app) I disabled autofocus to no avail. Also tried capture.set(cv.CAP_PROP_AUTOFOCUS, 0) with no luck either. I think that the problem is OpenCV as I did the following test: I used the camera with 4 apps at the same time: Teams, FaceTime, Logitune (image preview option) and my code. They all show the same image but my code is the only one showing a blurry background. See the image attached. (https://i.sstatic.net/AlQd6G8J.png)


Solution

  • In your Mac menu bar, click on the green camera icon. Make sure that your Python instance is selected, and then turn off portrait mode:

    Portrait mode selector