#!/usr/bin/python
# -*- coding: UTF-8 -*-
import pyautogui
from PIL import ImageGrab
from time import sleep
from History import *
import os
import tkinter as tk # 导入 Tkinter 库
from tkinter import ttk
from tkinter import *
import tkinter.messagebox
import traceback
# import TextTransAPI as TextT
import ctypes
from tkinter import filedialog
About = "图文精灵 \t版本号 v2.0 \n该软件遵守 MIT 开源协议\nhttps://github.com/Weeeendi/Picture2Text"
Shareble = 1
NeedExit = 0
# 默认配置项
default_srclang = 'auto'
default_lang = 'en'
current_lang = default_lang
default_theme = "light"
current_theme = default_theme
main_windowHeight = 300
main_windowWidth = 500
disconnect = 0
import pyperclip
from paddleocr import PaddleOCR
def image_to_text(file_path):
try:
words_buffer = ""
ocr = PaddleOCR(use_gpu=False) # need to run only once to download and load model into memory
result = ocr.ocr(file_path, rec=True)
for line in result[0]:
line = line[1][0] + "\n"
words_buffer += line
# 将文本黏贴至剪贴板
pyperclip.copy(words_buffer)
print(words_buffer)
return words_buffer
except:
return ""
##spam = pyperclip.paste()
def message_askyesno(root):
'''
# Gets the requested values of the height and width.
windowWidth = root.winfo_reqwidth()
windowHeight = root.winfo_reqheight()
# Gets both half the screen width/height and window width/height
positionRight = int(root.winfo_screenwidth()/2 - windowWidth/2)
positionDown = int(root.winfo_screenheight()/2 - windowHeight/2)
'''
root.withdraw() # ****实现主窗口隐藏
return (tk.messagebox.askyesno("提示", "要执行此操作?"))
def clearEdit(Editx):
Editx.delete('1.0', END)
# Editx.configure(fg='black') # 修改字体颜色,修改其它参数只需要传入对应的参数即可
# 图片识别的结果显示在Edit1
def OcrDisplayCallback(root, Edit1, Edit2):
clearEdit(Edit1)
clearEdit(Edit2)
Temptext = pyperclip.paste()
Edit1.insert(INSERT, Temptext)
root.UpdateBg("请打开图片或截图", "./res/image/background1.png")
# 保存到历史记录
# def TransCallback(Edit1, Edit2, fm):
# clearEdit(Edit2)
# var = TextT.TextTranslate(current_lang, Edit1.get('1.0', END))
# Edit2.insert(INSERT, var)
# fm.pack(side=LEFT, fill=BOTH, expand=YES)
# 语言菜单
src_languages = {"自动": "auto", "英语": "en", "简中": "zh", "日语": "jp", "西班牙语": "spa",
"韩语": "kor", "繁中": "cht", "意大利语": "it", "捷克语": "cs", "法语": "fra"}
dec_languages = {"英语": "en", "简中": "zh", "日语": "jp", "西班牙语": "spa",
"韩语": "kor", "繁中": "cht", "意大利语": "it", "捷克语": "cs", "法语": "fra"}
def get_real_resolution():
"""获取真实的分辨率"""
# hDC = win32gui.GetDC(0)
# 横向分辨率
w, h = pyautogui.size()
return w, h
class MyCapture:
def __init__(self, png, root):
# 变量X和Y用来记录鼠标左键按下的位置
self.X = tkinter.IntVar(value=0)
self.Y = tkinter.IntVar(value=0)
# 屏幕尺寸
# screenWidth = root.winfo_screenwidth()
# screenHeight = root.winfo_screenheight()
# 解决windows缩放问题
user32 = ctypes.windll.user32
user32.SetProcessDPIAware(2)
[screenWidth, screenHeight] = [user32.GetSystemMetrics(0), user32.GetSystemMetrics(1)]
# screenWidth,screenHeight = get_real_resolution()
# 创建顶级组件容器
self.top = tkinter.Toplevel(
root, width=screenWidth, height=screenHeight)
# 不显示最大化、最小化按钮
self.top.overrideredirect(True)
self.canvas = tkinter.Canvas(
self.top, bg='blue', width=screenWidth, height=screenHeight)
# 显示全屏截图,在全屏截图上进行区域截图
self.image = tkinter.PhotoImage(file=png)
self.canvas.create_image(
screenWidth // 2, screenHeight // 2, image=self.image)
def onRightButtonDown(event):
self.top.destroy()
os.remove(filename)
root.state('normal')
self.canvas.bind('<Button-3>', onRightButtonDown)
# 鼠标左键按下的位置
def onLeftButtonDown(event):
self.X.set(event.x)
self.Y.set(event.y)
# 开始截图
self.sel = True
self.canvas.bind('<Button-1>', onLeftButtonDown)
# 鼠标左键移动,显示选取的区域
def onLeftButtonMove(event):
if not self.sel:
return
global lastDraw
try:
# 删除刚画完的图形,要不然鼠标移动的时候是黑乎乎的一片矩形
self.canvas.delete(lastDraw)
except Exception as e:
pass
lastDraw = self.canvas.create_rectangle(
self.X.get(), self.Y.get(), event.x, event.y, outline='blue')
self.canvas.bind('<B1-Motion>', onLeftButtonMove)
# 获取鼠标左键抬起的位置,保存区域截图
def onLeftButtonUp(event):
self.sel = False
'''try:
self.canvas.delete(lastDraw)
except Exception as e:
pass
'''
sleep(0.1)
# 考虑鼠标左键从右下方按下而从左上方抬起的截图
left, right = sorted([self.X.get(), event.x])
top, bottom = sorted([self.Y.get(), event.y])
pic = ImageGrab.grab((left + 1, top + 1, right, bottom))
# 弹出保存截图对话框
file_path = './res/image/somefile.png'
pic.save(file_path, 'PNG')
sleep(0.1)
'''
fileName = tkinter.filedialog.asksaveasfilename(
title='保存截图', filetypes=[('image', '*.jpg *.png')])
if fileName:
pic.save(fileName)
'''
# 关闭当前窗口
self.top.destroy()
return pic
self.canvas.bind('<ButtonRelease-1>', onLeftButtonUp)
# 让canvas充满窗口,并随窗口自动适应大小
self.canvas.pack(fill=tkinter.BOTH, expand=tkinter.YES)
# 开始截图
# 判断是否有网络连接
def isConnected():
return True
application_path = "./res/image/"
iconFile = "icon.ico"
class _Main: # 调用SysTrayIcon的Demo窗口
def __init__(s):
s.SysTrayIcon = None # 判断是否打开系统托盘图标
# 初始化历史记录
Recordfile = os.path.join(os.extsep, *PATH)
s.histroyF = RecordHty(file=Recordfile + File)
def popup(s, event):
s.menu.post(event.x_root, event.y_root) # post在指定的位置显示弹出菜单
def setlang(s, event):
global current_lang
print(s.v.get())
current_lang = dec_languages.get(s.v.get())
def Edit_about(s, action, event=None):
'''option obtain "back","callback","clear","copy"."cut","paste",'''
# 撤销、重做
if (action == "back"):
try:
s.Edit1.edit_undo()
s.Edit2.edit_undo()
except Exception as e:
traceback.print_exc()
if (action == "callback"):
try:
s.Edit1.edit_redo()
s.Edit2.edit_redo()
except Exception as e:
traceback.print_exc()
if (action == "clear"):
try:
s.Edit1.delete('1.0', END)
s.Edit2.delete('1.0', END)
except Exception as e:
traceback.print_exc()
if (action == "cut"):
global root
s.Edit2.event_generate('<<Cut>>')
s.Edit1.event_genera