Get Text Size using OpenCV

Get Text Size using OpenCV

OpenCV has various drawing functions to draw geometric shapes such as line, rectangle, circle and write text on images.

The getTextSize function calculates and returns width and height of a text string.

import cv2

font = cv2.FONT_HERSHEY_SIMPLEX
fontScale = 1.0
thickness = 2
size, _ = cv2.getTextSize('Test', font, fontScale, thickness)
width, height = size

print(width, height)  # 65 22
#include <opencv2/opencv.hpp>

using namespace cv;

int main()
{
    int font = FONT_HERSHEY_SIMPLEX;
    double fontScale = 1.0;
    int thickness = 2;
    Size size = getTextSize("Test", font, fontScale, thickness, nullptr);

    std::cout << size.width << ' ' << size.height << std::endl; // 65 22

    return 0;
}
package app;

import org.opencv.core.*;
import org.opencv.imgproc.Imgproc;

public class Main
{
    static { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); }

    public static void main(String[] args)
    {
        int font = Imgproc.FONT_HERSHEY_SIMPLEX;
        double fontScale = 1.0;
        int thickness = 2;
        Size size = Imgproc.getTextSize("Test", font, fontScale, thickness, null);

        System.out.println(size.width + " " + size.height); // 65.0 22.0

        System.exit(0);
    }
}

Leave a Comment

Cancel reply

Your email address will not be published.