Learn how you can become a Python programmer in just 12 weeks.

    We respect your privacy. Unsubscribe at anytime.

    Find All Possible Webcam Resolutions with OpenCV in Python

    What will we cover in this tutorial?

    How to get a list of all possible webcam resolutions of your webcam using OpenCV. This is not straight forward as this feature is not supported by the OpenCV interface.

    Why is this not trivial?

    The quick answer is, there is so many different webcams that have different drivers. That means that webcams have different features, and can do different things, support different resolutions.

    Hence, OpenCV has made support for the most common things. Among those are the following.

    • cv.CAP_PROP_FRAME_WIDTH Width of the frames in the video stream.
    • cv.CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream.

    You can see the full list here.

    The things you can do is to call the get and set with these parameters. The get will get the current used value and set will set it.

    import cv2
    cap = cv2.VideoCapture(0)
    width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
    height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
    print(width, height)

    Which will with my laptop print.

    1280.0 720.0

    Hence, my webcam has a 1280×720 pixels resolution.

    To figure out all possible resolutions of your webcam, you would expect to find that as a possibility. But you will be disappointed.

    You can’t just set it to any resolution

    Webcams have a fixed set of possible pixels resolutions. The default for mine is 1280×720 pixels, but that does not mean it can only use that.

    Unfortunately, the diversity of possibilities of resolutions is big and differentiated and not standardized. Also, you cannot just set it as you want to.

    Let’s try that.

    import cv2
    cap = cv2.VideoCapture(0)
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, 300)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 400)
    width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
    height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
    print(width, height)

    We try to set it to 300×400 pixels to see what happens.

    640.0 480.0

    As the above shows, it sets it to 640×480 pixels.

    That means it sets it to some other possible resolution if you set it to something it does not support.

    So how to find out what resolutions are supported by your webcam?

    Good question. I thought you would never ask.

    Don’t we just do it like this.

    for width in range(1, 15360+1):
        for height in range(1, 8640+1):
             # Try the resolution to see if it works

    That is we try all possibilities all the way up to the highest resolution we know of (16K).

    Bad news. It is pretty slow to check if a resolution works, let’s just say it can try 100 per second. That would take 15360×8640 = 132710400 iterations. With 100 per second it would take 15.36 days to try out.

    So a better idea is to try out all know resolutions. We find a pretty good list on wikipedia of List of common resolutions.

    Using Pandas read_html to get the table and run through them all can be done like this.

    import pandas as pd
    import cv2
    url = "https://en.wikipedia.org/wiki/List_of_common_resolutions"
    table = pd.read_html(url)[0]
    table.columns = table.columns.droplevel()
    cap = cv2.VideoCapture(0)
    resolutions = {}
    for index, row in table[["W", "H"]].iterrows():
        cap.set(cv2.CAP_PROP_FRAME_WIDTH, row["W"])
        cap.set(cv2.CAP_PROP_FRAME_HEIGHT, row["H"])
        width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
        height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
        resolutions[str(width)+"x"+str(height)] = "OK"

    For my webcam it returns the following list.

      '320.0x240.0': 'OK', 
      '640.0x480.0': 'OK', 
      '1280.0x720.0': 'OK'

    And it took 8.28 seconds to try out the 193 possible resolutions from the list. That is less than 23 checks per second. Hence, the 100 checks seems to be quite optimistic.

    On my laptop it would take over 66 days to check them all out in the naive way.

    Python Circle

    Do you know what the 5 key success factors every programmer must have?

    How is it possible that some people become programmer so fast?

    While others struggle for years and still fail.

    Not only do they learn python 10 times faster they solve complex problems with ease.

    What separates them from the rest?

    I identified these 5 success factors that every programmer must have to succeed:

    1. Collaboration: sharing your work with others and receiving help with any questions or challenges you may have.
    2. Networking: the ability to connect with the right people and leverage their knowledge, experience, and resources.
    3. Support: receive feedback on your work and ask questions without feeling intimidated or judged.
    4. Accountability: stay motivated and accountable to your learning goals by surrounding yourself with others who are also committed to learning Python.
    5. Feedback from the instructor: receiving feedback and support from an instructor with years of experience in the field.

    I know how important these success factors are for growth and progress in mastering Python.

    That is why I want to make them available to anyone struggling to learn or who just wants to improve faster.

    With the Python Circle community, you can take advantage of 5 key success factors every programmer must have.

    Python Circle
    Python Circle

    Be part of something bigger and join the Python Circle community.

    Leave a Comment