1

How can I create an image which is transparent except the pixels of a given text in python opencv?

I have found this solution in C++ which creates an image without transparency. So when merging them together, the solution just adds the pixels. Which makes the text pixel intensity variable depending on the background image.

Mat img = imread("lenna.png", CV_LOAD_IMAGE_COLOR);

// Create and rotate the text
Mat textImg = Mat::zeros(img.rows, img.cols, img.type());
putText(textImg, "stackoverflow", Point(0, img.cols/2), FONT_HERSHEY_SIMPLEX, 2.0,Scalar(20,20,20),2);

// Sum the images (add the text to the original img)
img= img+textImg;

namedWindow("WaterMark", CV_WINDOW_AUTOSIZE);
imshow("WaterMark", img);

So, one way would be to make a mask image from the text and then use it to generate the alpha channel. But is there a better solution for this.

Sumsuddin Shojib
  • 3,583
  • 3
  • 26
  • 45

1 Answers1

4
image = np.zeros((100,300,4),np.uint8)
cv2.putText(image, "Test String", (10,50),cv2.FONT_HERSHEY_PLAIN,3,(0,0,255,255))

result

zindarod
  • 6,328
  • 3
  • 30
  • 58