python-3.x - 如何仅保留 pdf 文本或图像的注释部分?
问题描述
我已经构建了将 pdf 文本转换为图像的程序,我想只保留括号之间的文本部分:
例如,我要保留的文本的第一部分是:
我知道我可以使用Image
fromPIL
然后使用该.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)
解决方案
使用 cv2 (pip install opencv-python) 进行模板匹配或块检测
推荐阅读
- java - 在 java 8 上运行 JStack 会停止所有线程吗?
- java - 使用可比较的类进行选择排序
- java - 如何在 OnClickListener 中迭代我的变量?
- node.js - React/Node Uncaught SyntaxError: Unexpected token <
- javascript - 如何将数据库中的数据打印到模型弹出框?
- powershell - Powershell 中的慢速处理脚本,Workfow 第一步
- android - Android MainActivity 在回压后执行子活动操作
- javascript - 如何连接声音和图像
- asp.net-mvc - 如何更改 WebClient 类的标题?
- groovy - 如何在 Groovy 中声明一个字符串数组?