pythonimagejavax.imageioimage-capturepython-imageio

Changing image size while capturing using webcam using imageio


I am trying to capture image with each function call

import imageio as iio
camera = iio.get_reader("<video0>")
screenshot = camera.get_data(0)
plt.imsave(filename, screenshot)
camera.close()

I am able to save the image but the image size is varying sometime it is of the size of 960540 and sometime 1280720.

I went through the documentation of imageio but did not find any attribute to set its shape. I want a fix shape of image all the time.

I have tried OpenCV, it has its own limitation w.r.t my requirements.

So please suggest something in this package only.

Could anyone please help.


Solution

  • Your comment made clear you want to keep the resolution consistent for the duration of the whole video. As imageio does not provide a resize operation I suggest you to use skimage to handle that part.

    import imageio as iio
    import matplotlib.pyplot as plt
    from skimage.transform import resize
    
    camera = iio.get_reader("<video0>")
    
    filename = "experiment_"
    
    # randomly set to 30 frames for this example
    for i in range(0, 30):
        screenshot = camera.get_data(i)
        # skimage resize function here
        screenshot = resize(screenshot, (1280, 720))
        final_filename = str(i) + ".jpg"
        plt.imsave(filename+final_filename, screenshot)
    camera.close()