Skip to content
Related Articles

Related Articles

How to draw Filled rectangle to every frame of video by using Python-OpenCV?

View Discussion
Improve Article
Save Article
  • Last Updated : 27 Jan, 2022

In this article, we will discuss how to draw a filled rectangle on every frame of video through OpenCV in Python.

Stepwise Implementation:

  • Import the required libraries into the working space.
  • Read the video on which you have to write.


cap = cv2.VideoCapture(“path”)

  • Create an output file using cv2.VideoWriter_fourcc() method. Here you will have options of video formats.


output = cv2.VideoWriter(“output.avi”, cv2.VideoWriter_fourcc(*’MPEG’), 30, (1080, 1920))

  • Then edit the frames of the video by adding shapes to it (In our case it is a filled rectangle). We will use the cv2.rectangle() method. This method is used to draw a rectangle on any image.


cv2.rectangle(frame, (100, 100), (500, 500), (0, 255, 0), -1)

  • Then write all the frames in the video file.




In this example, we add a green rectangle to the video.

Input Video:


import cv2
def main():
    # reading the input
    cap = cv2.VideoCapture("input.mp4")
    output = cv2.VideoWriter(
        "output.avi", cv2.VideoWriter_fourcc(*'MPEG'), 
      30, (1080, 1920))
        ret, frame =
            # adding filled rectangle on each frame
            cv2.rectangle(frame, (100, 150), (500, 600),
                          (0, 255, 0), -1)
            # writing the new frame in output
            cv2.imshow("output", frame)
            if cv2.waitKey(1) & 0xFF == ord('s'):
if __name__ == "__main__":


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!