

- #PYTHON PUT TEXT ON IMAGE HOW TO#
- #PYTHON PUT TEXT ON IMAGE INSTALL#
- #PYTHON PUT TEXT ON IMAGE CODE#
Plugging this back into your original code: img = Image.new("L", (width, height), color=0) # "L": (8-bit pixels, black and white)ĭraw.text(((width-w)/2, (height-h)/2), text=text, fill='white', font=font) (The extra space for width is equally distributed so not interesting for centering). Knowing that line height is normally ~20% larger than the glyphs/characters (+ some trial and error), and we can figure out the extent of the extra space. if we create a box that is the exact size reported for your 'H' img = Image.new("L", (width, height), color=0) # "L": (8-bit pixels, black and white)įont = uetype("arial.ttf", font_size)
#PYTHON PUT TEXT ON IMAGE HOW TO#
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.Text always have some added space around characters, e.g.

You may have to find the shape of your specific text this using Paint or other application. The approximate shape of the text in the above example is (268, 36). Image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR) Python Write Text at the center of the image If you know the shape (width, height) of the text you are writing on the image, then you can place at center aligned on the image.
#PYTHON PUT TEXT ON IMAGE CODE#
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) In this step, the image on which we are going to add text is imported and open by using the Image.open (‘Imagename’). 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 The font file say cyrillic.ttf is kept locally to be used by your script. 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.
#PYTHON PUT TEXT ON IMAGE INSTALL#
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. Unfortunately, it is not possible in OpenCV to use a font beyond the standard set. 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.
