Search code examples
pythonpython-2.7opencvplotimshow

How to update plot window of image using opencv python at certain regular interval?


I've set of images which are dynamically being generated by another python script "image_generator.py" at some rate. I want to display these images dynamically. Is there any method so that I can update my image plot with newly generated image from "image_generator.py"?


Solution

  • image_generator.py

    import numpy
    
    
    def generate(height, width, channels):
        img = numpy.random.rand(height, width, channels)
        img = (255*img).astype(numpy.uint8)
        return img
    

    example.py

    import cv2
    from image_generator import generate
    
    n_images = 20
    height = 200
    width = 200
    channels = 3
    
    for n in range(n_images):
        img = generate(height, width, channels)
        cv2.imshow('Random image', img)
        k = cv2.waitKey(200)
        if k == ord('q') & 0xFF:
            break
    

    Note that this is no different than if they were in the same Python file. Once you import from another module, you have access to the functions as they it were sitting inside your Python script.