首页 > 解决方案 > 如何仅保留 pdf 文本或图像的注释部分?

问题描述

我已经构建了将 pdf 文本转换为图像的程序,我想只保留括号之间的文本部分:

在此处输入图像描述

例如,我要保留的文本的第一部分是:

在此处输入图像描述

我知道我可以使用ImagefromPIL然后使用该.crop(left, upper, right, lower)功能,但我不知道如何让它知道我想要[文本中存在的部分。

我知道即使我们按颜色提取图像,此链接也很有用。事实上,他们试图找到轮廓并迭代。如果他们找到 4 个角,他们会做另一件事来提取文本。这里我们有 2 个角

这是我从 pdf 格式的图像中获取文本的部分代码:

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)  

标签: python-3.ximage-processing

解决方案


使用 cv2 (pip install opencv-python) 进行模板匹配或块检测


推荐阅读