Hi I am using opencv to extract text in white from one image and paste it on another. I am using below approach for black image background and it works fine. Check the below code
import cv2
import numpy as np
image_msg = (30, 209, 187, 31)
base_path="/Users/images/"
mask_image1 = "{}show_image_2.PNG".format(base_path)
image_1_title = "{}show_image_2_logo.PNG".format(base_path)
img = cv2.imread(mask_image1)
image_1_title_img = cv2.imread(image_1_title)
image_1_titl_img = cv2.cvtColor(image_1_title_img, cv2.COLOR_BGR2RGB)
im1r_title = cv2.resize(image_1_titl_img, (image_msg[2], image_msg[3]))
print(im1r_title.shape)
plt.imshow(im1r_title,cmap='gray')
plt.axis('off')
plt.show()
print(img.shape)
plt.imshow(img,cmap='gray')
plt.axis('off')
plt.show()
img[image_msg[1]: image_msg[1] + image_msg[3],
image_msg[0]: image_msg[0] + image_msg[2],
] = np.where(im1r_title < [100, 100, 100], img[image_msg[1]: image_msg[1] + image_msg[3],
image_msg[0]: image_msg[0] + image_msg[2],
], im1r_title)
plt.imshow(img,cmap='gray')
plt.axis('off')
plt.show()
Result:
however I am struggling to find a way to get the text in white color from the below, if you open this image you will see text in white, how can I extract and paste on another image?
Problematic image with text:
That's because all the information is actually in the alpha/transparency channel which shows where white should show and where the black background should show.
If I separate your image into its constituent R,G,B and A channels and lay them out side-by-side you will see the info is in the A channel on the right:
I artificially added a red border so you can see its full extent on StackOverflow's pesky white background.
You will need to open it with:
im = cv.imread(..., cv.IMREAD_UNCHANGED)[..., 3]