Final Fit of Logo in OpenCV

Last Updated : 13 Feb, 2026

Final Fit of Logo means resizing and placing a logo so it stays clear and proportional when the frame size changes. It prevents blurring by adjusting the logo size and position dynamically using the cv2.resize() method with a scaling factor.

cv2.resize()

Below is the syntax for resize() function:

cv.resize(src, dsize)

Parameters:

  • src: Input image (video frame or logo)
  • dsize: Tuple (width, height) for new size

Python Implementation

The following code demonstrates how to maintain a logo’s size and position dynamically while resizing the frame. The logo is automatically scaled and placed at the top-right corner of the live webcam feed.

Python
import cv2 as cv
import numpy as np

cam = cv.VideoCapture(0)
scaling_factor = 1.0    
logo = cv.imread('logo.png')

while True:
    logo = cv.imread('logo.png')       
    _, img = cam.read()
    img = cv.flip(img, 1)              

    img = cv.resize(img, (int(img.shape[1] * scaling_factor), int(img.shape[0] * scaling_factor)))
    ht, wt = img.shape[:2]

    logo_height = int(ht * 0.12)       # 12% of frame height
    logo_width  = int(wt * 0.18)       # 18% of frame width

    if logo_width > wt - 10:
       logo_width = wt - 10
    if logo_height > ht - 10:
       logo_height = ht - 10

    logo_resized = cv.resize(logo, (logo_width, logo_height))

    margin_top = 10
    margin_left = wt - 10 - logo_width  

    img[margin_top:margin_top+logo_height, margin_left:margin_left+logo_width] = logo_resized

    cv.imshow('Frame', img)

    key = cv.waitKey(1)
    if key == ord('s'):      # Press 's' to decrease scaling
        scaling_factor -= 0.1
    elif key == ord('w'):    # Press 'w' to increase scaling
        scaling_factor += 0.1
    elif key == 27:          # ESC to exit
        cam.release()
        break

cv.destroyAllWindows()

Output

The webcam feed displays the logo at the top-right corner, and it automatically resizes and repositions according to the frame size.

FinalFit
Output of Final Fit

Explanation:

  • cv.resize() is used to resize both the frame and the logo proportionally.
  • The logo’s width and height are calculated as percentages of the current frame (logo_height = 12%, logo_width = 18%).
  • margin_left and margin_top ensure the logo stays inside the top-right corner without overflowing.
  • Reloading the logo every frame preserves quality, preventing blurriness when resizing.
  • Press 'w' or 's' to increase or decrease the frame size interactively; the logo adapts automatically.
Comment