Scale factor that is multiplied by the font-specific base size.Ī Scalar object specifying the color of the text. Integer constant specifying the font of the text. It accepts −Īn empty mat object to store the source image.Ī string object to specify the desired text.Ī Point object specifying the position of the text. This method renders the specified text in the given image. import numpy as np import cv2 image cv2.imread sample.png,cv2.IMREADUNCHANGED) position (10,50) cv2.putText ( image, numpy array on which text is written 'Python Examples', text position, position at which writing has to start cv2.FONTHERSHEYSIMPLEX, font family 1, font size (209, 80, 0, 255), font. With this article at OpenGenus, you must have the complete idea of how to write Text on image in custom font in Python using PIL and OpenCV.You can add text to an image using the putText() method of the class. ![]() Image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR) Once everything is done, save the image with text: image = omarray(image)įollowing is the complete Python code to write Text on image in custom font in Python using PIL and OpenCV: import cv2 You need to switch to the next line with a new dimension to accomodate longer sentences.Ĭonvert the image back to OpenCV format if you need to use OpenCV function for some functions: image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR) ![]() If the text cannot fit on a single line on the image, then part of the text that goes outside the image is not printed. Note that the text will be written on a single line irrespective of the length of the text. You shall give the point correctly to have the text on the correct location. ![]() (width, height) is the left top-most point of the text. Load the image in the form required by ImageDraw of PIL: image = np.load("image.jpeg")įont = uetype("localfont.ttf", font_size)ĭraw the text on the image: text = "opengenus"ĭraw.text((width, height), str(text), font=font) Import the relevant libraries: import cv2 This can again be simply added using the function cv2. Now, lets get to the meat of the post - adding text to the image. The font file say cyrillic.ttf is kept locally to be used by your script. cv2.putText () - Add text to resized image. Usually, it is downloadable in OTF format so you need to convert it to TTF format. ![]() You need to convert the font file to TTF format. To install PIL in your system: pip install pillowĭownload the font file of the Font you want to use. Once the text is written over the image using PIL, the image can be transferred back to be used by OpenCV. The alternative is to use Python Imaging Library (PIL) which has the support to use custom fonts to write text over images. Steps: Draw different style texts using cv2.putText () Create window using cv2.namedWindow () Display Image using cv2.imshow () Save the output in an image file using cv2.imwrite () Wait for keyboard button press using cv2.waitKey () Exit window and destroy all windows using cv2. Syntax: cv2. cv2.putText () method is used to draw a text string on any image. Unfortunately, it is not possible in OpenCV to use a font beyond the standard set. OpenCV-Python is a library of Python bindings designed to solve computer vision problems. OpenCV provides a set of standard fonts that a developer can use to write text on an image. In this article, we have explained how to use custom font in Python, OpenCV to write text over an image.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |