Monday, 15 August 2016

TUT10: Drawing TEXT on canvas

Hello guys,
          In this tutorial, we are going to see how can we actually write a text like this one below on a canvas(drawing window).



Here's the code:


import cv2
import numpy as np
img=np.zeros((500,500,3), np.uint8)
font= cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img, 'ankit',(100,400), font, 4, (255,0,255), 2, cv2.LINE_AA)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Explanation

Line1-2: import necessary packages(see previous tutorials for info on them)

Line 3: Create a canvas using zeros function 

Line 4: We need to specify a font we wish to draw in. I encourage you to see list of fonts available in              cv2 module to try with other fonts too.

Line 5: puttext() is the function that takes in various parameters as follows:
           1) canvas 
           2) String which you want to draw
           3) starting co-ordinate of the string
           4) font type which we stored in variable font
           5) font size
           6) color
           7) thickness of font(dont confuse with size)
           8) line type(click to know more)

Line 6: Show the image

Line 7-8: Hold the screen using waitKey(0) and exit all windows after the canvas is closed


Output:


That's it for this tutorial. See you in next tutorial which is going to be little interesting.

Any queries, please ask in the comment section below.

No comments:

Post a comment