how to remove text from image (not inpaiting but just solid color over the text)

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
enter image description here

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))```

  • After you get your points, use fillPoly to make a mask (as you have alread), then you can fill the img with white using the mask and numpy. img_text_removed = img.copy(), then img_text_removed[mask==255] = (255,255,255)

    – 

Leave a Comment