Search code examples
pythonopencvframes

How to display frame on fixed location in opencv python


I am working on a project where I am reading image file and displaying some information of that image on the frame. As of now I am displaying all the information on the frame itself due to which it is not looking good:

enter image description here

I thought of making the background of the information black so that it will be more readable but because of the black background, it was covering some useful parts of the image. So I thought of why not lets break the frame into two parts. On the left side, we will display all the information with black background and on the right side we will display full frame image. Something like below:

enter image description here

I am not sure if showing above frame is possible or not because in future I might be showing frames from the video/camera feed. If there is anyone who has already done this, then please let me know. Please help. Thanks

Below is the code I am using:

import cv2
import imutils

frame = cv2.imread('image.jpg')
frame = imutils.resize(frame, width=800, height=480)

cv2.putText(frame, "Some information", (5, 30), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 1)
cv2.putText(frame, "More information", (5, 60), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 1)
cv2.putText(frame, "Some more information", (5, 90), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 1)

cv2.imshow('Application', frame)
key = cv2.waitKey(0)

Solution

  • import cv2
    import numpy as np
    
    frame = cv2.imread('image.jpg')
    left = np.zeros((frame.shape[0], 400, frame.shape[2]), dtype=frame.dtype)
    
    cv2.putText(left, "Some information", (5, 30), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 1)
    cv2.putText(left, "More information", (5, 60), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 1)
    cv2.putText(left, "Some more information", (5, 90), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 1)
    
    img = np.hstack((left,frame))
    
    cv2.imshow('Application', img)
    

    enter image description here