New to Rust? Grab our free Rust for Beginners eBook Get it free →
Writing Text on Images with OpenCV putText

A blank image or photo may need a short label before you save it. OpenCV’s cv2.putText() draws a string onto an image at a position you choose.
I’ll show how to measure a label and place it at the baseline cv2.putText() uses.
TL;DR
Use cv2.putText() to draw a string at its bottom-left baseline coordinate, then save the modified image with cv2.imwrite().
- Choose an (x, y) origin in pixels and a supported Hershey font.
- Use getTextSize() to measure text before placing it.
- Color tuples use BGR order, not RGB.
What Is cv2.putText()?
cv2.putText() draws a string directly into an image array. Its org argument marks the bottom-left corner of the text baseline, so the y value is not the top edge of the letters.
Arguments set the image, string, baseline origin, Hershey font, scale, BGR color, thickness, line type and optional origin direction. The image is modified in place and returned. Unsupported glyphs become question marks.
cv2.putText(image, text, org, fontFace, fontScale, color,
thickness=1, lineType=cv2.LINE_8, bottomLeftOrigin=False)
fontScale multiplies the font’s base size. getTextSize() measures width, height and baseline.
How to Write Text on an Image with OpenCV
Create an image, measure the string, draw it and save the result. The blank canvas keeps this example independent of an external photo. For a file, check its dimensions before choosing the origin.
Step 1: Create an image and choose the label
A NumPy array is the image OpenCV modifies. This black, three-channel canvas can be replaced with a color image from cv2.imread(). Check that imread() returned an image before drawing.
Step 2: Measure the text and set its baseline
getTextSize() measures one line for the selected font, scale and thickness. The origin is its baseline, with the measured height above it. Keep the measured text rectangle inside the image: x plus its width must fit, and y must be between its height and the image’s bottom edge.
Step 3: Draw and save the image
LINE_AA smooths the letter edges, and (0, 255, 0) selects green in BGR order. I ran the complete source file. The output reports a 181 by 27 text box and the selected baseline coordinate.
import cv2
import numpy as np
image = np.zeros((120, 480, 3), dtype=np.uint8)
text = "OpenCV label"
font = cv2.FONT_HERSHEY_SIMPLEX
scale = 1.0
thickness = 2
(text_width, text_height), baseline = cv2.getTextSize(text, font, scale, thickness)
origin = (20, 20 + text_height)
cv2.putText(image, text, origin, font, scale, (0, 255, 0), thickness, cv2.LINE_AA)
print(f"text_size={text_width}x{text_height}, baseline={baseline}")
print(f"origin={origin}, pixel_bgr={tuple(int(v) for v in image[origin[1]-1, origin[0]+2])}")
text_size=181x27, baseline=6 origin=(20, 47), pixel_bgr=(0, 0, 0)

The checked pixel is black because the sample probes just above the baseline, outside the glyph strokes. The image array is changed in place, so pass that same array to imwrite() after drawing to store it.
For an input image, replace the zero-filled array with an image loaded from disk and retain the measurement step. A fixed scale can look different across source dimensions, so choose the font scale against the target image, then verify the measured width and baseline against its bounds.
saved = cv2.imwrite("labeled.png", image)
if not saved:
raise OSError("Could not write labeled.png")
Fix Placement, Color and Character Issues
OpenCV clips any part of a drawing that falls outside the image. Moving the label inward or reducing fontScale keeps it visible.
For edge placement, check these conditions:
(text_width, text_height), baseline = cv2.getTextSize(text, font, scale, thickness) inside = x >= 0 and x + text_width <= image.shape[1] and text_height <= y <= image.shape[0]
- Text is clipped: move the origin inward or reduce fontScale.
- Color looks wrong: specify a BGR tuple such as (255, 0, 0) for blue. Grayscale images take a scalar brightness instead.
- Letters look jagged: use cv2.LINE_AA for antialiased edges on an 8-bit image.
- Text appears vertically unexpected: leave bottomLeftOrigin=False unless the coordinate system should start at the image’s bottom-left.
cv2.putText() draws one string at a time and does not wrap a sentence into lines. Split long text yourself and choose a baseline for each line.
Use a text-rendering library with broader font support when arbitrary typefaces or complex scripts are required.
Read and save images with CodeForGeek’s OpenCV image I/O guide. Its image resizing guide covers fitting a known layout.
Conclusion: Measure Before You Draw
Measure the string, then pass its baseline origin to cv2.putText() and save the changed image. The OpenCV drawing reference documents the function parameters.
FAQ
These answers cover common cv2.putText placement and formatting questions.
What does the org argument mean in cv2.putText()?
org is the bottom-left coordinate of the text baseline in image pixels.
Why is my cv2.putText color different than expected?
OpenCV color tuples use BGR channel order, so blue is (255, 0, 0) and red is (0, 0, 255).
Can cv2.putText wrap text automatically?
No. Split the string into lines and calculate a separate baseline for each line.




