.
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QDockWidget, QTextEdit, QAction, QFileDialog, QMessageBox, QVBoxLayout, QWidget, QPushButton, QLabel, QHBoxLayout
from PyQt5.QtCore import Qt, QRect, QPoint, pyqtSignal
from PyQt5.QtGui import QPixmap, QPainter, QColor, QPen, QCursor
from PIL import Image
class NotePad(QMainWindow):
def init(self):
super().init()
self.image = None
self.pixmap = None
self.start_point = None
self.end_point = None
self.rect = None
self.scale_factor = 1.0
self.crosshair_enabled = False
self.last_cursor_pos = None
self.dragging = False
self.drag_offset = QPoint()
self.selecting = False
初始化日志标签
self.log_label = QLabel(self)
self.log_label.setFixedSize(300, 300) # 调整日志区域大小
self.log_label.setStyleSheet("border: 1px solid black; background-color: white;")
self.log_label.setAlignment(Qt.AlignTop)
self.log_label.setVisible(True)
self.initUI()
def initUI(self):
self.setWindowTitle("NotePad")
self.setGeometry(100, 100, 1000, 700)
self.textEdit = QTextEdit()
self.setCentralWidget(self.textEdit)
dock = QDockWidget("File Actions", self)
dock.setAllowedAreas(Qt.LeftDockWidgetArea)
#openButton = QPushButton("Open File")
#openButton.clicked.connect(self.openFile)
#saveButton = QPushButton("Save File")
#saveButton.clicked.connect(self.saveFile)
ocrButton = QPushButton("OCR Check")
ocrButton.clicked.connect(self.ocrCheck)
layout = QVBoxLayout()
#layout.addWidget(openButton)
#layout.addWidget(saveButton)
layout.addWidget(ocrButton)
widget = QWidget()
widget.setLayout(layout)
dock.setWidget(widget)
self.addDockWidget(Qt.LeftDockWidgetArea, dock)
添加日志显示区域
logDock = QDockWidget("Log", self)
logDock.setAllowedAreas(Qt.RightDockWidgetArea)
logDock.setWidget(self.log_label) # 确保此处使用了正确的属性
self.addDockWidget(Qt.RightDockWidgetArea, logDock)
图片显示区域
self.image_label = QLabel(self)
self.image_label.setGeometry(100, 100, 800, 600)
self.image_label.setAlignment(Qt.AlignCenter)
self.image_label.mousePressEvent = self.mouse_press
self.image_label.mouseMoveEvent = self.mouse_move
self.image_label.mouseReleaseEvent = self.mouse_release
self.image_label.setMouseTracking(True) # 允许鼠标跟踪
按钮布局
button_layout = QHBoxLayout()
fit_button = QPushButton("Fit to Window")
fit_button.clicked.connect(self.fit_to_window)
zoom_in_button = QPushButton("Zoom In")
zoom_in_button.clicked.connect(self.zoom_in)
zoom_out_button = QPushButton("Zoom Out")
zoom_out_button.clicked.connect(self.zoom_out)
crosshair_button = QPushButton("Crosshair")
crosshair_button.clicked.connect(self.toggle_crosshair)
drag_button = QPushButton("Drag Image")
drag_button.clicked.connect(self.enable_dragging)
select_button = QPushButton("Select Area")
select_button.clicked.connect(self.enable_selecting)
button_layout.addWidget(fit_button)
button_layout.addWidget(zoom_in_button)
button_layout.addWidget(zoom_out_button)
button_layout.addWidget(crosshair_button)
button_layout.addWidget(drag_button)
button_layout.addWidget(select_button)
将按钮布局添加到主窗口
main_layout = QVBoxLayout()
main_layout.addLayout(button_layout)
main_layout.addWidget(self.image_label)
central_widget = QWidget()
central_widget.setLayout(main_layout)
self.setCentralWidget(central_widget)
self.createActions()
self.createMenus()
self.show()
def createActions(self):
self.openAction = QAction("Open File", self)
self.openAction.triggered.connect(self.openFile)
self.saveAction = QAction("Save File", self)
self.saveAction.triggered.connect(self.saveFile)
def createMenus(self):
self.fileMenu = self.menuBar().addMenu("File")
self.fileMenu.addAction(self.openAction)
self.fileMenu.addAction(self.saveAction)
def openFile(self):
fileName, _ = QFileDialog.getOpenFileName(self, "Open File", "", "Text Files (*.txt);;All Files (*)")
if fileName:
try:
with open(fileName, 'r', encoding='utf-8') as file:
content = file.read()
self.textEdit.setPlainText(content)
except Exception as e:
QMessageBox.critical(self, "Error", str(e))
def saveFile(self):
fileName, _ = QFileDialog.getSaveFileName(self, "Save File", "", "Text Files (*.txt);;All Files (*)")
if fileName:
try:
with open(fileName, 'w', encoding='utf-8') as file:
content = self.textEdit.toPlainText()
file.write(content)
QMessageBox.information(self, "Success", "File saved successfully.")
except Exception as e:
QMessageBox.critical(self, "Error", str(e))
def ocrCheck(self):
fileName, _ = QFileDialog.getOpenFileName(self, "Open Image", "", "Image Files (*.jpg *.jpeg *.png)")
if fileName:
try:
self.image = Image.open(fileName)
self.pixmap = QPixmap(fileName)
self.fit_to_window() # 初始加载时就适应窗口
self.scale_factor = 1.0
except Exception as e:
QMessageBox.critical(self, "Error", str(e))
def mouse_press(self, event):
if self.dragging and event.button() == Qt.LeftButton:
self.drag_offset = event.pos() - self.image_label.pos()
elif self.selecting and event.button() == Qt.LeftButton:
self.start_point = event.pos()
self.end_point = event.pos()
self.update()
def mouse_move(self, event):
if self.dragging and event.buttons() & Qt.LeftButton:
self.image_label.move(event.pos() - self.drag_offset)
elif self.selecting and self.start_point and event.buttons() & Qt.LeftButton:
self.end_point = event.pos()
if self.crosshair_enabled:
self.last_cursor_pos = event.pos()
self.update()
def mouse_release(self, event):
if self.selecting and self.start_point and self.end_point:
rect = QRect(self.start_point, self.end_point).normalized()
self.rect = (rect.x(), rect.y(), rect.width(), rect.height())
self.check_red_color()
self.update()
def check_red_color(self):
if self.image and self.rect:
cropped_image = self.image.crop(self.rect)
rgb_image = cropped_image.convert('RGB')
pixels = list(rgb_image.getdata())
for pixel in pixels:
r, g, b = pixel
if r > 200 and g < 100 and b < 100: # 红色的标准可以调整
self.show_check_ng()
return
self.show_check_ok()
def show_check_ng(self):
self.log_label.setText("Check NG\n" + self.log_label.text())
QMessageBox.warning(self, "Result", "Check NG")
def show_check_ok(self):
self.log_label.setText("Check OK\n" + self.log_label.text())
QMessageBox.information(self, "Result", "Check OK")
def paintEvent(self, event):
painter = QPainter(self.image_label)
if self.selecting and self.start_point and self.end_point:
pen = QPen(Qt.red, 2, Qt.SolidLine)
painter.setPen(pen)
painter.drawRect(QRect(self.start_point, self.end_point).normalized())
if self.crosshair_enabled and self.last_cursor_pos:
pen = QPen(Qt.red, 1, Qt.SolidLine)
painter.setPen(pen)
x, y = self.last_cursor_pos.x(), self.last_cursor_pos.y()
painter.drawLine(x, 0, x, self.image_label.height())
painter.drawLine(0, y, self.image_label.width(), y)
def fit_to_window(self):
if self.pixmap:
self.scale_factor = 1.0
self.image_label.setPixmap(self.pixmap.scaled(self.image_label.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation))
self.image_label.adjustSize()
def zoom_in(self):
if self.pixmap:
self.scale_factor *= 1.25
scaled_pixmap = self.pixmap.scaled(self.pixmap.size() * self.scale_factor, Qt.KeepAspectRatio, Qt.SmoothTransformation)
self.image_label.setPixmap(scaled_pixmap)
self.image_label.adjustSize() # 适应新的尺寸
def zoom_out(self):
if self.pixmap:
self.scale_factor /= 1.25
scaled_pixmap = self.pixmap.scaled(self.pixmap.size() * self.scale_factor, Qt.KeepAspectRatio, Qt.SmoothTransformation)
self.image_label.setPixmap(scaled_pixmap)
self.image_label.adjustSize() # 适应新的尺寸
def toggle_crosshair(self):
self.crosshair_enabled = not self.crosshair_enabled
if self.crosshair_enabled:
self.setCursor(Qt.CrossCursor)
else:
self.setCursor(Qt.ArrowCursor)
def enable_dragging(self):
self.dragging = True
self.selecting = False
self.setCursor(Qt.OpenHandCursor) # 改变光标为拖动状态
def enable_selecting(self):
self.dragging = False
self.selecting = True
self.setCursor(Qt.ArrowCursor) # 改变光标为默认状态
if name == 'main':
app = QApplication(sys.argv)
notepad = NotePad()
sys.exit(app.exec_())