I am trying to fetch selected text by bounding box on an Image. like if only on word is selected by bounding box and I want to fetch that text and convert it into the text file. Please see my code and give some review so I can implement that functionality.
So far what I've done I've converted the PDF file to image with bounding box over the text.
import numpy as np
import csv
import io
from PIL import Image
import pytesseract
from wand.image import Image as wi
from pytesseract import Output
import cv2
pdf = wi(filename="samplecompany.pdf", resolution=100)
pdfImg = pdf.convert('jpg')
j = 1
for img in pdfImg.sequence:
page = wi(image=img)
page.save(filename=str(j)+".jpg")
img1 = cv2.imread(str(j)+".jpg")
d = pytesseract.image_to_data(img1, output_type=Output.DICT)
n_boxes = len(d['level'])
print(n_boxes)
for i in range(n_boxes):
(x, y, w, h) = (d['left'][i], d['top']
[i], d['width'][i], d['height'][i])
print((x, y, w, h))
cv2.rectangle(img1, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imwrite(str(j)+".jpg", img1)
cv2.waitKey(0)
j += 1
this code is working fine I need to fetch desired text from images which I've created.using bounding box location