一、前期准备
需要先下载 tesseract-ocr-w64-setup-v5.0.1.20220118 ,官网下载即可,尽量用最新的版本,旧版本识别率很低并且解压后不能自动下载相关内容。解压后路径需要替换下文C:\Program Files\Tesseract-OCR\tesseract.exe。可以提取视频中的文本,其他的一般。
二、代码
注释代码如下:
"""
增加图片处理库PIL
pip install Pillow
增加OCR识别库
pip install pytesseract
"""
from PIL import Image
import pytesseract as pt
import tkinter as tk
import tkinter.filedialog as filedialog
class Application(tk.Tk):
def __init__(self):
super().__init__()
self.title("图片文本提取")
pt.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
self.img_path = tk.StringVar()
self.frame = tk.Frame(self)
self.frame.pack(padx=10, pady=10)
self.lbl_file = tk.Label(self.frame, text="图像")
self.lbl_file.grid(row=0, column=0)
self.txt_file = tk.Entry(self.frame, width=60, textvariable=self.img_path)
self.txt_file.grid(row=0, column=1, sticky=tk.W)
self.btn_file = tk.Button(self.frame, text="选择", command=self.sel_img_file)
self.btn_file.grid(row=0, column=1, sticky=tk.E)
self.lbl_txt = tk.Label(self.frame, text="文本")
self.lbl_txt.grid(row=1, column=0)
self.txt_exract = tk.Text(self.frame)
self.txt_exract.grid(row=1, column=1)
self.btn_extract = tk.Button(self.frame, text="提取文本", command=self.extract_text)
self.btn_extract.grid(row=2, column=1, sticky=tk.W+tk.E)
def sel_img_file(self):
self.img_path.set(filedialog.askopenfilename(title="选择图片", initialdir="."))
def extract_text(self):
if self.img_path:
img = Image.open(self.img_path.get())
text = pt.image_to_string(img, lang="chi_sim")
self.txt_exract.delete(1.0, tk.END)
self.txt_exract.insert(tk.END, text)
if __name__ == "__main__":
app = Application()
app.mainloop()
|