Hi have the following python code that find the text in a image, and uses cv2.inpaint to remove that text by covering with generated background, but I just want to put solid color over the text instead generating the background
this is what I want to achieve

import keras_ocr
import cv2
import math
import numpy as np
def inpaint_text(img_path, pipeline):
img = keras_ocr.tools.read(img_path)
prediction_groups = pipeline.recognize([img])
inpainted_img = ""
mask = np.zeros(img.shape[:2], dtype="uint8")
for box in prediction_groups[0]:
x0, y0 = box[1][0]
x1, y1 = box[1][1]
x2, y2 = box[1][2]
x3, y3 = box[1][3]
points = np.array([[x0, y0], [x1, y1], [x2, y2],[x3, y3]])
cv2.fillPoly(mask,np.int32([points]), 255)
inpainted_img = cv2.inpaint(img, mask, 0, cv2.INPAINT_NS)
return(inpainted_img)
pipeline = keras_ocr.pipeline.Pipeline()
img_text_removed = inpaint_text('image.png', pipeline)
plt.imshow(img_text_removed)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('result.png', cv2.cvtColor(img_text_removed, cv2.COLOR_BGR2RGB))```
>Solution :
import keras_ocr
import cv2
import numpy as np
import matplotlib.pyplot as plt
def inpaint_text(img_path, pipeline):
img = keras_ocr.tools.read(img_path)
prediction_groups = pipeline.recognize([img])
inpainted_img = img.copy() # Create a copy of the original image
mask = np.zeros(img.shape[:2], dtype="uint8")
color = (0, 0, 255) # Define the color you want to use for covering the text
for box in prediction_groups[0]:
x0, y0 = box[1][0]
x1, y1 = box[1][1]
x2, y2 = box[1][2]
x3, y3 = box[1][3]
points = np.array([[x0, y0], [x1, y1], [x2, y2], [x3, y3]])
cv2.fillPoly(inpainted_img, np.int32([points]), color) # Fill the area with the specified color
return inpainted_img
pipeline = keras_ocr.pipeline.Pipeline()
img_text_removed = inpaint_text('image.png', pipeline)
plt.imshow(cv2.cvtColor(img_text_removed, cv2.COLOR_BGR2RGB))
plt.axis('off')
plt.show()
cv2.imwrite('result.png', img_text_removed)
In this code, the primary change is that we directly modify the inpainted_img using the cv2.fillPoly() function to fill the text area with the specified color (color = (0, 0, 255) in this case). The cv2.cvtColor() function is not needed when saving the image, as we’re already working with the BGR format. Additionally, I’ve added plt.axis('off') to turn off the axis display when showing the image with matplotlib.