I have a diagram in a PDF format. I am using pdfminer.six
to extract the text present in the diagram as well as the bounding boxes of the text. Everything is fine so far.
System info: Windows 10, Python 3.9.13
Now I want to draw these bounding boxes on an image of the pdf and create a visualization using OpenCV rectangle()
. I am unsure about how to do this as the DPI is needed to convert the pdf to an image using pdf2image
.
Can anyone tell me how to draw this visualization using the bounding box data given by pdfminer
?
Code
I am providing an example code of the bounding box extraction using pdfminer
as well as a sample output to show how the bounding boxes are returned.
from pdfminer.high_level import extract_pages
from pdfminer.layout import LTTextBox, LAParams
import cv2 as cv
import os
import numpy as np
path = r"sample.pdf"
assert os.path.exists(path), "image path is wrong"
laparams = LAParams(detect_vertical=True)
for page_layout in extract_pages(path, laparams=laparams):
for element in page_layout:
if isinstance(element, LTTextBox):
print(element.bbox)
A snapshot of the output I am getting is as follows:
....
(64.46833119999998, 758.4685649600001, 143.16671221999994, 763.35576496)
(399.3279, 797.7414805999999, 464.28060816000004, 812.3556692000001)
(520.1078, 797.7414805999999, 631.1472937599999, 812.3556692000001)
(676.9479, 797.7414805999999, 762.4986252799999, 812.3556692000001)
(709.8279, 787.0014805999999, 729.9863304, 792.5868806)
....