如何使用Python将png图片转换为文本?

问题描述 投票:-2回答:1

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

经过几次尝试,我可以将扫描的PDF转换为PNG图像文件,之后,我很奇怪,有谁可以帮助我将PNG文件转换为WordPDF搜索。我的代码附后

请参考附件中的图片。

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图片转换成文字。

如何将所有的图片转换并保存在CSVword中?

python-3.x python-tesseract pytesser img2pdf
1个回答
0
投票

我得到了答案,我的问题

from PIL import Image
from pdf2image import convert_from_path
import pytesseract


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 += 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()
© www.soinside.com 2019 - 2024. All rights reserved.