python-3.x 使用pdf 2 image和table-transformers时,如何将图像坐标转换为PDF坐标?

dauxcl2d  于 2023-05-23  发布在  Python
关注(0)|答案(2)|浏览(207)

我正在使用pdf2image将pdf转换为图像,并使用表格转换器检测表格。我需要你帮我定位。
问题是,我得到了完美的表格边框,但图像中的像素与PDF坐标不同。如何将图像坐标转换为PDF坐标?下面是我的代码以供参考:

  1. from pdf2image import convert_from_path
  2. images = convert_from_path('/content/Sample Statement Format Bancslink.pdf')
  3. for i in range(len(images)):
  4. images[i].save('/content/pages_sbi/page'+str(i)+'.jpeg')
icnyk63a

icnyk63a1#

下面是如何使用PyMuPDF将图像坐标转换回PDF页面坐标。
这当然是一页一页地工作。因此,在下文中,假设图像文件是从对应的页面制成的。

  1. import fitz # PyMuPDF import
  2. doc = fitz.open("input.pdf")
  3. page = doc[pno] # page number pno is 0-based
  4. image = f"image{pno}.jpg" # filename of the matching image of the page
  5. # rectangle, e.g. one that wraps a table in the image
  6. # x0, y0 are coordinates of its top-left point
  7. # x1, y1 is the bottom-right point
  8. rect = fitz.Rect(x0, y0, x1, y1)
  9. # make a PyMuPDF iamge from the JPEG
  10. pix = fitz.Pixmap(image)
  11. # make a matrix that converts any image coordinates to page coordinates
  12. mat = pix.irect.torect(page.rect)
  13. # now every image coordinate can be converted to page coordinates
  14. # e.g. this is the table rect in page coordinates:
  15. pdfrect = rect * mat
  16. # if you don't want PyMuPDF objects as rectangle, just use
  17. # tuple(pdfrect) to retrieve the 4 coordinates

顺便说一句,PyMuPDF还能够将页面渲染为图像。因此,如果你的表检测机制可以逐页调用,你可以这样做一个循环:
1.使用PyMuPDF读取页面
1.将页面转换为图像。可能也在记忆里。
1.将页面图像传递给表识别器,后者返回表坐标
1.使用表格坐标并将其转换为页面坐标,如上所示。

展开查看全部
zxlwwiss

zxlwwiss2#

好吧,找到了完美的解决方案,几乎可以解决所有问题。
请将以下代码视为PDF to Image的代码:

  1. from pdf2image import convert_from_path
  2. images = convert_from_path('PATH')
  3. !mkdir pages
  4. for i in range(len(images)):
  5. images[i].save('/content/pages/page'+str(i)+'.jpeg')

现在,您需要首先获取PDF的数据:

  1. from pypdf import PdfReader
  2. reader = PdfReader('PATH')
  3. box = reader.pages[0].mediabox
  4. pdf_width = box.width
  5. pdf_height = box.height

现在读取并获取有关图像的数据:

  1. import cv2
  2. im = cv2.imread('/content/pages/page0.jpeg')
  3. height, width, channels = im.shape

现在考虑x_1,x_2,y_1和y_2作为图像中的坐标。要在PDF中获取相同的位置,请使用以下代码:

  1. x_1 = x_1/width*pdf_width
  2. y_1 = y_1/width*pdf_width
  3. x_2 = x_2/width*pdf_width
  4. y_2 = y_2/width*pdf_width

将此坐标用于您的工作。

展开查看全部

相关问题