from PIL import Image
import os
import pytesseract
import cv2 as cv
import fitz
def pdf_image(pdfPath,imgPath,zoom_x,zoom_y,rotation_angle):
# 打開PDF文件
pdf = fitz.open(pdfPath)
# 逐頁讀取PDF
for pg in range(0, pdf.pageCount):
page = pdf[pg]
# 設(shè)置縮放和旋轉(zhuǎn)系數(shù)
trans = fitz.Matrix(zoom_x, zoom_y).preRotate(rotation_angle)
pm = page.getPixmap(matrix=trans, alpha=False)
# 開始寫圖像
pm.writePNG(imgPath+str(pg)+".png")
#pm.writePNG(imgPath)
pdf.close()
pdf_path ='D:/123.pdf'
img_path ='D:/123.png'
pdf_image(pdf_path,img_path,5,5,0)
# 依賴opencv
img=cv.imread(img_path)
text=pytesseract.image_to_string(Image.fromarray(img),lang='chi_tra')
# 不依賴opencv寫法
# text=pytesseract.image_to_string(Image.open(img_path))
print(text)
以上就是如何使用Python進行PDF圖片識別OCR的詳細內(nèi)容,更多關(guān)于python pdf圖片識別ocr的資料請關(guān)注腳本之家其它相關(guān)文章!