How to retain only the annotated parts of a pdf text or an image of it?

Viewed 66

I have already built the program that turns the pdf text into an image and I would like to retain only the parts of a text that are between brackets :

enter image description here

For instance the first part of the text I want to retain would be :

enter image description here

I know I can I use Image from PIL and then the .crop(left, upper, right, lower) function but I don't know how to let it know I want the part where there is the [ in the text.

I know this link can be useful even if we extract an image by color. Indeed, they utry to find the contours and iterating through. If they find 4 corners, they do another stuff that leads to extracting the text. Here we have 2 corners

Here is part of the code I did to get the text from the image in the pdf form :

for file_name in file_names:
    # load the image and convert it to grayscale
    image = cv2.imread(file_name)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # check to see if we should apply thresholding to preprocess the
    # image
    if args["preprocess"] == "thresh":
        gray = cv2.threshold(gray, 0, 255,
            cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]

    # make a check to see if median blurring should be done to remove
    # noise
    elif args["preprocess"] == "blur":
        gray = cv2.medianBlur(gray, 3)

    # write the grayscale image to disk as a temporary file so we can
    # apply OCR to it
    filename = "{}.png".format(os.getpid())
    cv2.imwrite(filename, gray)

    # load the image as a PIL/Pillow image, apply OCR, and then delete
    # the temporary file
    text = pytesseract.image_to_string(Image.open(filename))
    os.remove(filename)
    #print(text)

    with open('resume.txt', 'a+') as f:
        print('***:', text, file=f)  
1 Answers

use cv2 (pip install opencv-python) for either template matching or block detection

Related