ubuntu20:pyqt5 截屏,图片识别文字
前置条件
ubuntu 20.04 安装pyqt5
import sys
sys.path.append("/home/wargame/.local/lib/python3.8/site-packages")
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class WScreenShot(QWidget):
def __init__(self, parent=None):
super(WScreenShot, self).__init__(parent)
self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
self.setStyleSheet('''background-color:black; ''')
self.setWindowOpacity(0.4)
# 1 透明度的有效范围从1.0 完全不透明到0.0 完全透明
# 2 默认情况下是不透明状态,属性值为1.0
desktopRect = QDesktopWidget().screenGeometry()
# 获取屏幕的信息
# screenGeometry()函数提供有关可用屏幕几何的信息
self.setGeometry(desktopRect)
# setGeometry (9,9, 50, 25)
# 从屏幕上(9,9)位置开始(即为最左上角的点),显示一个50*25的界面(宽50,高25)
# setGeometry之后一定要调用show函数,否则可能看不到控件存在
self.setCursor(Qt.ArrowCursor) # 设置鼠标形状
self.blackMask = QBitmap(desktopRect.size())
self.blackMask.fill(Qt.black)
self.mask = self.blackMask.copy()
self.isDrawing = False
self.startPoint = QPoint()
self.endPoint = QPoint()
self.setMouseTracking(False)
self.startX, self.startY = 0, 0 # the point where you start
self.endX, self.endY = 0, 0 # the point where you end
def paintEvent(self, event):
if self.isDrawing:
self.mask = self.blackMask.copy()
pp = QPainter(self.mask)
pen = QPen()
pen.setStyle(Qt.NoPen)
pp.setPen(pen)
brush = QBrush(Qt.white)
pp.setBrush(brush)
pp.drawRect(QRect(self.startPoint, self.endPoint)) # 画图
self.setMask(QBitmap(self.mask))
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.startPoint = event.pos()
self.startX, self.startY = event.x(), event.y()
# print("mousepress: ", self.startPoint)
self.endPoint = self.startPoint
self.isDrawing = True
def mouseMoveEvent(self, event):
if self.isDrawing:
self.endPoint = event.pos()
self.endX, self.endY = event.x(), event.y()
self.update()
def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton:
self.endPoint = event.pos()
# print("mouse release: ", self.endPoint)
# PySide2
# screenshot = QPixmap.grabWindow(QApplication.desktop().winId())
# PyQt5
# screenshot = QApplication.primaryScreen().grabWindow(0)
# 通用
screenshot = QApplication.primaryScreen().grabWindow(QApplication.desktop().winId())
# rect = QRect(self.startPoint, self.endPoint)
rect = QRect(self.startX, self.startY + 28, self.endX-self.startX, self.endY-self.startY)
# print("last rect:", rect)
outputRegion = screenshot.copy(rect)
outputRegion.save('111.jpg', format='JPG', quality=100)
# print("ok")
self.close()
def screenshot():
app = QApplication(sys.argv)
win = WScreenShot()
win.show()
app.exec_()
if __name__ == '__main__':
screenshot()
添加快捷键执行,执行完毕后会在家目录下生成一个固定名字的jpg图片
对该图片进行识别
tesseract-ocr 安装
cnocr
这个效果比较好
from cnocr import CnOcr
ocr = CnOcr()
res = ocr.ocr('111.jpg')
print("Predicted Chars:", res)
预测后直接将预测结果复制到剪贴板,可以直接使用, 或者调用驱动浏览器搜索
注意:
pyqt5 代码来源于网络,获取鼠标的位置不准确,手动调整了参数,需要熟悉qt5 后更新, 完善后可以生成可执行文件后使用