【问题标题】:How to convert .png images to text using Python如何使用 Python 将 .png 图像转换为文本
【发布时间】:2019-10-30 14:09:43
【问题描述】:

最近,我接了一个项目。使用 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 格式?

【问题讨论】:

  • 如果您在此处复制/粘贴代码会更好。如果您描述当前遇到的结果/错误也会很有帮助。

标签: python-3.x python-tesseract pytesser img2pdf


【解决方案1】:

我的问题得到了答案

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()

【讨论】:

  • 它会给我们提供可搜索的 pdf 或只是使用 tessseract ocr 转换的文本文件
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-13
  • 1970-01-01
  • 2022-07-05
  • 2018-04-03
  • 2021-06-11
  • 1970-01-01
相关资源
最近更新 更多