Search code examples
python-imageio

Set webcam grabbing resolution with ImageIO in Python


Using simple Windows/Python to read from Webcam:

camera = iio.get_reader("<video0>")
screenshot = camera.get_data(0)
camera.close()

I'm getting a default resolution of 1980x1920. The webcam had different, larger resolutions available. How do I set that UP?

ALSO - How do I set exposure time? image comes out pretty dark.

Thanks


Solution

  • You can set the resolution via the size kwarg, e.g. size=(1280, 720)

    My webcam is my third device and the resolution defaults to 640x360 but has 1280x720 available, so I would do something like:

    import imageio.v3 as iio
    
    frame = iio.imread("<video2>", index=0, size=(1280, 720))
    

    On a tangent, I'd also suggest switching to the easier iio.imiter for stream reading. It tends to produce cleaner code than the old iio.get_reader syntax.

    import imageio.v3 as iio
    
    for idx, frame in enumerate(iio.imiter("<video0>", size=(1980, 1920))):
        ... # do something with the frame
    
        if idx == 9:
            # read 10 frames
            break
    

    Response to your edit:

    Setting webcam exposure is a question that actually hasn't come up yet. Webcams typically feature automatic brightness adjustment, but that might take a few frames depending on the webcam's quality.

    Manual adjustment might already be possible and I just don't know about it (never looked into it). This is a separate question though and is probably better tracked as a new issue over at the ImageIO repo.