import os
import pytesseract
from PIL import Image
from tkinter import Tk, filedialog, Button, Text, END, Menu, Frame,messagebox,Label,PhotoImage
from datetime import datetime
import tkinter as tk
content_text = ""
def select_folder():
root = Tk()
root.withdraw() # 隐藏主窗口
file_path = filedialog.askopenfilename(filetypes=[("Image files", "*.jpg *.png")]) # 打开文件选择对话框
img = PhotoImage(file=file_path)
return file_path
def show_popup(event):
# 显示弹出菜单
popup_menu.post(event.x_root, event.y_root)
def copy_text(event=None):
# 获取选中的文本
selected_text = text_box.selection_get()
# 将选中的文本复制到剪贴板
text_box.clipboard_clear()
text_box.clipboard_append(selected_text)
def process_images(file_path):
img = Image.open(file_path).convert('L')
text = pytesseract.image_to_string(img, lang='chi_sim')
return text
def on_button_click():
file_path = select_folder()
if file_path:
text = process_images(file_path)
if text:
# 将识别出的文本显示在文本框中
text_box.delete(1.0, END) # 清空文本框
text_box.insert(END, text) # 插入新文本
else:
text_box.delete(1.0, END) # 清空文本框
text_box.insert(END, '扫描文本内容为空') # 插入新文本
else:
text_box.delete(1.0, END) # 清空文本框
text_box.insert(END, '未选择图片文件') # 插入新文本
def clear_text():
text_box.delete(1.0, END) # 清空文本框
def save_content():
text = text_box.get('1.0', 'end-1c') # 获取文本框内容
if text:
# 获取当前时间并格式化为字符串
current_time = datetime.now().strftime("%Y%m%d_%H%M%S")
# 定义文件名和目录
directory = "log"
file_name = f"{current_time}.txt"
file_path = os.path.join(directory, file_name)
# 获取文件的绝对路径
root_file_path = os.path.abspath(file_name)
# 确保目录存在
os.makedirs(directory, exist_ok=True)
# 尝试将文本内容写入到以时间命名的文件中
try:
with open(file_path, 'w', encoding='utf-8') as file:
file.write(text)
# 弹出信息对话框
messagebox.showinfo("保存成功", f"本地文件路径为:{root_file_path}")
except Exception as e:
# 弹出警告对话框
messagebox.showwarning("Save Error", f"Failed to save the content: {e}")
else:
print("has no content to save.")
if __name__ == "__main__":
# 创造GUI界面
root = Tk()
root.title("OCR 图像文本识别")
# 设置窗口大小
root.geometry("700x550")
# 定义按钮
open_button = Button(root, text="打开图像", command=on_button_click)
open_button.pack(pady=20)
# 创建按钮框架
content_frame = Frame(root)
content_frame.pack(fill=tk.BOTH, expand=True)
# 定义文本框
text_box = Text(content_frame, wrap='word', width=70, height=20, border=0)
text_box.pack(side="left", fill=tk.BOTH, expand=True, padx=5)
# 创建按钮框架
button_frame = Frame(root)
button_frame.pack(pady=10)
# 定义清空按钮和保存按钮,并放在同一行显示
clear_button = Button(button_frame, text="清空内容", command=clear_text)
clear_button.pack(side="left", padx=5)
save_button = Button(button_frame, text="保存为TXT", command=save_content)
save_button.pack(side="left", padx=5)
# 创建弹出菜单
popup_menu = Menu(root, tearoff=0)
popup_menu.add_command(label="Copy", command=copy_text)
# 绑定鼠标右键点击事件到show_popup函数
text_box.bind("[B]", show_popup)
#运行打包EXE命令:pyinstaller --onefile --noconsole ocr_script.py
root.mainloop()
运行截图
image.png (20.56 KB, 下载次数: 0)
下载附件
2024-11-15 10:31 上传
image.png (29.69 KB, 下载次数: 0)
下载附件
2024-11-15 10:32 上传
image.png (37.69 KB, 下载次数: 0)
下载附件
2024-11-15 10:33 上传