Python 3.x 如何使用Python将.png图像转换为文本
最近,我参加了一个项目。使用Python tesseract将扫描的PDF转换为可搜索的PDF/word 经过几次尝试,我可以将扫描的PDF转换为PNG图像文件,之后,我被谁能帮我将PNG文件转换为Word/PDF searchable感到震惊 请查看附件中的图片以供参考Python 3.x 如何使用Python将.png图像转换为文本,python-3.x,python-tesseract,pytesser,img2pdf,Python 3.x,Python Tesseract,Pytesser,Img2pdf,最近,我参加了一个项目。使用Python tesseract将扫描的PDF转换为可搜索的PDF/word 经过几次尝试,我可以将扫描的PDF转换为PNG图像文件,之后,我被谁能帮我将PNG文件转换为Word/PDF searchable感到震惊 请查看附件中的图片以供参考 Import os Import sys from PIL import image Import pytesseract from pytesseract import image_to_string Libpath =
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
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,还是只提供一个使用Tesseract ocr转换的文本文件