当前位置:   article > 正文

pdf坐标及其显示(深入研究)_wps pdf 文字坐标

wps pdf 文字坐标

这一篇文章紧接着上一篇文章深入研究,上一篇文章如下。https://blog.csdn.net/k650d/article/details/126794818?spm=1001.2014.3001.5502

上一篇文章想要观察pdf文件的坐标需要在Acrobat观察,并不直观,我们可以将pdf转换为图片,并在图片中展示坐标方框(opencv),图片使用opencv库和pdf文件使用pdfminer库或者pdfplumber库,需要注意的是pdf转换为图片的时候需保持像素一直,即图片的长和宽的像素一致。pdf转换为图片的时候使用的是fitz,pdfplumber本身也能转换,但是图片质量太差了(像素一致)。代码如下。

  1. import pdfplumber
  2. import fitz
  3. import numpy as np
  4. import cv2
  5. list3 = []
  6. path = r'you.pdf'
  7. with pdfplumber.open(path) as pdf:
  8. # 获取第一页
  9. page = pdf.pages[0]
  10. width = page.width
  11. height = page.height
  12. # ================================================
  13. for i in page.chars: # .rects .chars
  14. list3.append([i['x0'] ,height - i['y1'], i['x1'] , height - i['y0']])
  15. # ======================pdf转换==========================
  16. pdf_document = fitz.open(path)
  17. page = pdf_document[0] # 获取当前页
  18. image = page.get_pixmap() # 将当前页转换为图像
  19. image_data = image.samples # 获取图像数据
  20. image_np = np.frombuffer(image_data, dtype=np.uint8).reshape(image.height, image.width, 3) # 将图像数据转换为NumPy数组
  21. image_cv2 = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
  22. # ====================所有的字符显示=======================
  23. for j in list3:
  24. top_left = (int(j[0]), int(j[1])) # 左上角的坐标 (x, y)
  25. bottom_right = (int(j[2]), int(j[3])) # 右下角的坐标 (x, y)
  26. color = (0, 255, 0) # BGR颜色,这里是绿色
  27. thickness = 1 # 线宽
  28. cv2.rectangle(image_cv2, top_left, bottom_right, color, thickness)
  29. cv2.imshow(r'result', image_cv2)
  30. cv2.waitKey(0)

上述代码中的.chars可以自由替换为下面图片中的类,以展示所有pdf文件内的所有结构元素。

如果想要在图片中画框,显示其坐标,也可以使用这段代码。代码如下。

  1. import cv2
  2. import random
  3. import argparse
  4. clicked = False
  5. g_rectangle = [0, 0, 0, 0]
  6. g_startPoint = [0, 0]
  7. def onMouse(event, x, y, flags, param):
  8. global clicked
  9. global g_rectangle
  10. global g_startPoint
  11. if event == cv2.EVENT_MOUSEMOVE:
  12. if clicked == True:
  13. g_rectangle[0] = min(g_startPoint[0], x)
  14. g_rectangle[1] = min(g_startPoint[1], y)
  15. g_rectangle[2] = max(g_startPoint[0], x)
  16. g_rectangle[3] = max(g_startPoint[1], y)
  17. print(g_startPoint)
  18. # 左键按下事件
  19. if event == cv2.EVENT_LBUTTONDOWN:
  20. g_startPoint[0] = x
  21. g_startPoint[1] = y
  22. clicked = True
  23. # 左键弹起事件
  24. if event == cv2.EVENT_LBUTTONUP:
  25. print("====================选中框的坐标:===========================")
  26. print("矩形框左上角坐标:")
  27. print(g_rectangle[0], g_rectangle[1])
  28. print("矩形框右下角坐标:")
  29. print(g_rectangle[2], g_rectangle[3])
  30. clicked = False
  31. def startRoi(path):
  32. cv2.namedWindow("MyWindow", 0)
  33. cv2.resizeWindow("MyWindow", 1280, 720) # 设置长和宽
  34. cv2.setMouseCallback("MyWindow", onMouse)
  35. # 按"Esc"退出
  36. print("Press Esc if you want to exit ...")
  37. while cv2.waitKey(30) != 27:
  38. global frame
  39. frame = cv2.imread(path)
  40. # 画矩形
  41. cv2.rectangle(frame, (g_rectangle[0], g_rectangle[1]), (g_rectangle[2], g_rectangle[3]), (0, 255, 0), 2)
  42. cv2.imshow("MyWindow", frame)
  43. cv2.destroyWindow("MyWindow")
  44. if __name__ == '__main__':
  45. path = r"you.jpg"
  46. startRoi(path)

 pdfplumber中的表格也有其坐标,注意这里是坐标而不是字符串列表输出。表格的坐标和上述代码中的.chars,.rects等类的坐标原点不一致,部分代码如下。

  1. # =========================表格的坐标=====================
  2. # 获取页面的所有表格
  3. tables = page.find_tables()
  4. for table in tables:
  5. for cell in table.cells:
  6. print(cell)
  7. # =========================坐标添加=====================
  8. list5.extend([[int(list(cell)[1]), int(list(cell)[3]), int(list(cell)[0]), int(list(cell)[2])]])

pdf文件及其扫描版pdf文件(测试用例和人工执行测试用例扫描版)位置发生了一些变换,通过平移和倾斜使其结构相似度最高,然后再做处理(检查扫描版pdf人工填写的一些错误)。代码如下,遍历平移和倾斜求最高结构相似度。

  1. import cv2
  2. import numpy as np
  3. from skimage.metrics import structural_similarity as ssim
  4. # 读取两张图片
  5. img1 = cv2.imread(r"you.jpg", cv2.IMREAD_GRAYSCALE)
  6. img2 = cv2.imread(r"you.jpg", cv2.IMREAD_GRAYSCALE)
  7. # 初始化最佳SSIM值和最佳变换参数
  8. best_ssim = -1
  9. best_translation = (0, 0)
  10. best_rotation = 0
  11. # 定义平移和倾斜的范围
  12. translation_range = 8
  13. rotation_range = 2
  14. # 遍历平移和倾斜范围
  15. for dx in range(0, translation_range+1):
  16. for dy in range(0, translation_range+1):
  17. for angle in range(0, rotation_range+1):
  18. # 平移和旋转图像
  19. M = cv2.getRotationMatrix2D((img2.shape[1] / 2, img2.shape[0] / 2), angle, 1)
  20. img2_translated = cv2.warpAffine(img2, M, (img2.shape[1], img2.shape[0]))
  21. img2_translated = np.roll(img2_translated, dx, axis=1)
  22. img2_translated = np.roll(img2_translated, dy, axis=0)
  23. # 计算SSIM值
  24. score, _ = ssim(img1, img2_translated, full=True)
  25. # 更新最佳SSIM值和参数
  26. if score > best_ssim:
  27. best_ssim = score
  28. best_translation = (dx, dy)
  29. best_rotation = angle
  30. print("最佳SSIM值:", best_ssim)
  31. print("最佳平移:", best_translation)
  32. print("最佳倾斜:", best_rotation)
  33. # 应用最佳平移和倾斜参数
  34. M = cv2.getRotationMatrix2D((img2.shape[1] / 2, img2.shape[0] / 2), best_rotation, 1)
  35. img2 = cv2.warpAffine(img2, M, (img2.shape[1], img2.shape[0]))
  36. img2 = np.roll(img2, best_translation[0], axis=1)
  37. img2 = np.roll(img2, best_translation[1], axis=0)
  38. # 显示具有最佳SSIM值时的图像
  39. cv2.imwrite(r"you.jpg", img2)
  40. cv2.imshow('Best Translated Image (SSIM: %.2f)' % best_ssim, img2)
  41. cv2.waitKey(0)
  42. cv2.destroyAllWindows()

经过几千页的测试用例的矫正,即pdf文件及其扫描版pdf文件(测试用例和人工执行测试用例扫描版)参数比对,发现上述遍历平移和旋转的方法,效率低(即便多线程提升效率,也较慢),正确率不够高,正确率在90%以上,但无法达到接近100%。于是提出通过仿射变换矫正扫描版pdf文件,提取最大方框的四角坐标,执行仿射变换。如何拿到准确的四角坐标是一件困难的事情,这里不做解释。

  1. import pdfplumber
  2. import fitz
  3. import cv2
  4. import numpy as np
  5. import math
  6. # ================================================
  7. path = r'pdf原件位置'
  8. with pdfplumber.open(path) as pdf:
  9. # 获取第一页
  10. page = pdf.pages[0]
  11. # ================================================
  12. tables = page.find_tables()
  13. for table in tables:
  14. table1 = table.cells[0]
  15. table2 = table.cells[-1]
  16. goal_top_left_tuple = (table1[0], table1[1])
  17. goal_top_right_tuple = (table2[2], table1[1])
  18. goal_bottom_left_tuple = (table1[0], table2[3])
  19. goal_bottom_right_tuple = (table2[2], table2[3])
  20. # ================================================
  21. # 打开 PDF 文件
  22. pdf_document = fitz.open(r'pdf原件扫描版位置')
  23. # 选择要处理的页面(例如第一页)
  24. page = pdf_document[0]
  25. # ================================================
  26. image = page.get_pixmap() # 将当前页转换为图像
  27. image_data = image.samples # 获取图像数据
  28. image_np = np.frombuffer(image_data, dtype=np.uint8).reshape(image.height, image.width, 3) # 将图像数据转换为NumPy数组
  29. image_cv2 = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR) # 将图像数据转换为OpenCV格式
  30. # ================================================
  31. # 转换为灰度图像
  32. gray_image = cv2.cvtColor(image_cv2, cv2.COLOR_BGR2GRAY)
  33. _, binary = cv2.threshold(gray_image, 200, 255, cv2.THRESH_BINARY) # 二值化处理
  34. # 使用 Canny 边缘检测算法
  35. edges = cv2.Canny(binary, 50, 150)
  36. # 查找轮廓
  37. contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  38. # 初始化极值点
  39. top_left = (0, 0) # 初始化为左上角
  40. top_right = (binary.shape[1], 0) # 初始化为右上角
  41. bottom_left = (0, binary.shape[0]) # 初始化为左下角
  42. bottom_right = (binary.shape[1], binary.shape[0]) # 初始化为右下角
  43. # ================================================
  44. # 找到高度大于一定值的轮廓的极值点
  45. list1=[]
  46. list2=[]
  47. list3=[]
  48. list4=[]
  49. for contour in contours:
  50. for point in contour:
  51. x, y = point[0].astype(float)
  52. if 80 < y < binary.shape[0] - 80: # 设定高度阈值为50(可以根据需要调整)
  53. top_left_distance = math.sqrt((x - top_left[0])**2 + (y - top_left[1])**2)
  54. list1.append((top_left_distance , x ,y))
  55. top_right_distance = math.sqrt((top_right[0] - x)**2 + (y - top_right[1])**2)
  56. list2.append((top_right_distance, x, y))
  57. bottom_left_distance = math.sqrt((x - bottom_left[0])**2 + (bottom_left[1] - y)**2)
  58. list3.append((bottom_left_distance, x, y))
  59. bottom_right_distance = math.sqrt((bottom_right[0] - x)**2 + (bottom_right[1] - y)**2)
  60. list4.append((bottom_right_distance, x, y))
  61. a = min(list1, key=lambda x:x[0])
  62. b = min(list2, key=lambda x:x[0])
  63. c = min(list3, key=lambda x:x[0])
  64. d = min(list4, key=lambda x:x[0])
  65. top_left_tuple = (a[1],a[2])
  66. top_right_tuple = (b[1],b[2])
  67. bottom_left_tuple = (c[1],c[2])
  68. bottom_right_tuple = (d[1],d[2])
  69. # ================================================
  70. # 定义原始图像中四个角的坐标
  71. pts_src = np.array([top_left_tuple, top_right_tuple, bottom_left_tuple, bottom_right_tuple], dtype=np.float32)
  72. # 定义目标图像中的四个角点,使其形成一个矩形
  73. points_dst = np.array([goal_top_left_tuple, goal_top_right_tuple, goal_bottom_left_tuple, goal_bottom_right_tuple], dtype=np.float32)
  74. # 计算仿射变换矩阵
  75. matrix = cv2.getPerspectiveTransform(pts_src, points_dst)
  76. # 应用仿射变换
  77. image_corrected = cv2.warpPerspective(image_cv2, matrix, (image_cv2.shape[1], image_cv2.shape[0]))
  78. # 显示校正后的图像
  79. cv2.imshow('Corrected Image', image_corrected)
  80. cv2.waitKey(0)
  81. cv2.destroyAllWindows()

如下是效果图对比,左边是处理后的图像,右边是扫描pdf文件。

在处理pdf文件数据的时候还发现一件事情,两张图片对比,一张在人眼看上去不相似的局部图像结构相似度竟然很高,结构相似度只关注代表结构构成的部分图像,然后我自定义了一个相似度计算方法。另外fitz,win10和win7不兼容,需要相应版本的库,需要注意。

我现在的编程方式,文心一言,chatgpt,智谱一起跑,都是很好的老师,不知道哪一款gpt可以胜出。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/744750
推荐阅读
相关标签
  

闽ICP备14008679号