Skip to content

Instantly share code, notes, and snippets.

@yptheangel
Created August 2, 2021 16:01
Show Gist options
  • Save yptheangel/36b06ef56a34919c486bfacc162a7154 to your computer and use it in GitHub Desktop.
Save yptheangel/36b06ef56a34919c486bfacc162a7154 to your computer and use it in GitHub Desktop.
add watermark to image using opencv by using steps: insert text at center of image, blend two images together to make watermark transparent
import cv2
filename = "yourfilename.jpg"
frame = cv2.imread(filename)
overlay = frame.copy()
text = "I love opencv"
font, font_scale, font_thickness = cv2.FONT_ITALIC, 3.5, 5
textsize = cv2.getTextSize(text, font, font_scale, font_thickness)[0] # get text size
h, w = frame.shape[0], frame.shape[1] # get height, width of original image
# get target point to insert text at center of image
Px = (w - textsize[0]) // 2 # centerX minus x offset
Py = (h + textsize[1]) // 2 # centerY minus y offset
# create background for your text
cv2.rectangle(overlay, (Px, Py), (Px + textsize[0] , Py - textsize[1]), (220,0,0), -1)
cv2.putText(overlay, text, (Px, Py), font, font_scale, (255, 255, 255), font_thickness)
# blend your original and edited frames
alpha = 0.3 # adjust to control blend
cv2.addWeighted(overlay, alpha, frame, 1-alpha, 0, frame)
cv2.imshow("output", frame)
cv2.waitKey(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment