Python 图片文字识别 OCR:批量提取图片里的文字
import os
import pytesseract
from PIL import Image
# Windows 需指定 tesseract 路径
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
def ocr_folder(folder):
for name in os.listdir(folder):
if not name.lower().endswith(('.png', '.jpg', '.jpeg')):
continue
path = os.path.join(folder, name)
text = pytesseract.image_to_string(Image.open(path), lang='chi_sim+eng')
print(f"--- {name} ---\n{text}\n")
ocr_folder('./截图')
import pytesseract
from PIL import Image
# Windows 需指定 tesseract 路径
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
def ocr_folder(folder):
for name in os.listdir(folder):
if not name.lower().endswith(('.png', '.jpg', '.jpeg')):
continue
path = os.path.join(folder, name)
text = pytesseract.image_to_string(Image.open(path), lang='chi_sim+eng')
print(f"--- {name} ---\n{text}\n")
ocr_folder('./截图')

更新时间:2026-09-14 13:36:59