Lee
Lee

Reputation: 11

How to add text over an image

I need help in Python/, how do you add strings over an empty canvas.

Can anyone help me please? Thanks

Upvotes: 0

Views: 766

Answers (1)

Mika72
Mika72

Reputation: 411

import numpy as np
import cv2

img = cv2.imread('myimg.jpg',cv2.IMREAD_COLOR)

h,w,_ = img.shape

font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img,'My Text Here',(w/2,h/2), font, 1, (200,255,155), 2, cv2.LINE_AA)

cv2.namedWindow('My Image With Text', cv2.WINDOW_NORMAL)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

cv2.imwrite('myimg_with_text.jpg',img)

You have to adjust text placement by half of height and lenngth (in pixels). Get dimensions of the bounding box of text object 1st...

In old version of OpenCV

h,w = img.shape

works instead of

h,w,_ = img.shape

Upvotes: 1

Related Questions