使用 Python tesseract 从 PNG 图像中提取文本

Extract text from PNG images using Python tesseract

最近接了一个项目。使用 Python tesseract 将扫描的 PDF 转换为可搜索的 PDF/word。

经过几次尝试,我能够将扫描的 PDF 转换为 PNG 图像文件,之后,我很惊讶有人可以帮助我将 PNG 文件转换为 Word/PDF 可搜索的。my piece of code attached

请找到附件图片以供参考。

Import os
Import sys
from PIL import image
Import pytesseract
from pytesseract import image_to_string

 Libpath =r'_______' #site-package
 Pop_path=r'_______' #poppler dlls
 Sys.path.insert(0,LibPath)

  from pdf2image import convert_from_path

     Pdfpath=r'_______' # PDF file directory
     imgpath=r'_______' #image output path

     images= convert_from_path(pdf_path = pdfpath, 
         dpi=500, poppler_path= pop_path)
      for idx, of in enumerate (images):
                 pg.save(imgPath+'PDF_Page_'+'.png',"PNG")
                 print('{} page converted'.format(str(idx)))

       try:
          from PIL import image
       except ImportError:
                 import image
         import pytesseract

     def ocr-core(images):
              Text = 
       pytesseract.image_to_string(image.open(images))
       return text
  print(ocr_core("image path/imagename))

就是这样,我写了.....然后我得到了多个“.PNG”图像...现在我只能将一个 PNG 图像转换为文本。

如何将所有图片转换并保存在CSV/word中?

  from PIL import image
  from pdf2image import convert_from_path
  import pytesseract
  import OS
  import sys

   Pdf_file_path = '_______' #your file path

  Images = convert_from_path(Pdf_file_path, dpi=500)

Counter=1
for page in Images:
       idx= "image_"+str(Counter)+".jpg" ##or ".png"
       page.save(idx, 'JPEG')
       Counter = Counter+1

 file=Counter-1
  Output= '_____' #where you want to save and file name
 f=open(output, "w")
 for i in range(1,file+1):
          idx= "image_"+str(Counter)+".jpg" ##or ".png"         
 text=str(pytesseract.image_to_string(Image.open(idx)))
     f.write(text)
     f.close()