新建一个pyqt5,要求实现以下功能 1、有按键“打开文件夹”,可自主选定文件夹 2、有按键“画矩形框”,可对选定的文件夹中所有图片进行用鼠标画一个任意大小的红色矩形框的操作,每次只对一张图片进行操作 3、有按键“保存文件夹”,可自主选定文件夹 4、有按键“保存”,可将画了矩形框的图片保存到“保存文件夹”中 5、有按键“下一张”,可对下一张图片进行操作
时间: 2023-11-27 16:51:55 浏览: 99
可以参考以下代码来实现这个功能:
```python
import sys
import os
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap, QPainter, QColor, QPen
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog, QPushButton, QLabel
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# 初始化变量
self.image_path = None
self.save_path = None
self.image_index = 0
self.rect = None
# 设置窗口标题和大小
self.setWindowTitle('标注工具')
self.setGeometry(100, 100, 800, 600)
# 创建按键和标签控件
self.open_btn = QPushButton('打开文件夹', self)
self.open_btn.setGeometry(10, 10, 100, 30)
self.open_btn.clicked.connect(self.open_folder)
self.rect_btn = QPushButton('画矩形框', self)
self.rect_btn.setGeometry(120, 10, 100, 30)
self.rect_btn.clicked.connect(self.draw_rect)
self.save_folder_btn = QPushButton('保存文件夹', self)
self.save_folder_btn.setGeometry(230, 10, 100, 30)
self.save_folder_btn.clicked.connect(self.save_folder)
self.save_btn = QPushButton('保存', self)
self.save_btn.setGeometry(340, 10, 100, 30)
self.save_btn.clicked.connect(self.save_image)
self.next_btn = QPushButton('下一张', self)
self.next_btn.setGeometry(450, 10, 100, 30)
self.next_btn.clicked.connect(self.next_image)
self.image_label = QLabel(self)
self.image_label.setGeometry(10, 50, 780, 540)
self.image_label.setAlignment(Qt.AlignCenter)
def open_folder(self):
# 打开文件夹对话框
folder_path = QFileDialog.getExistingDirectory(self, '选择文件夹')
if folder_path:
# 获取文件夹中所有图片的路径
image_list = [os.path.join(folder_path, i) for i in os.listdir(folder_path) if i.endswith('.jpg') or i.endswith('.png')]
if image_list:
self.image_path = image_list
self.load_image()
def load_image(self):
# 加载当前图片并显示
pixmap = QPixmap(self.image_path[self.image_index])
self.image_label.setPixmap(pixmap)
def draw_rect(self):
# 开始画矩形框
self.image_label.mousePressEvent = self.mouse_press_event
self.image_label.mouseReleaseEvent = self.mouse_release_event
def mouse_press_event(self, event):
# 记录鼠标按下的位置
self.start_pos = event.pos()
def mouse_release_event(self, event):
# 记录鼠标释放的位置,并画出矩形框
self.end_pos = event.pos()
self.rect = (self.start_pos.x(), self.start_pos.y(), self.end_pos.x()-self.start_pos.x(), self.end_pos.y()-self.start_pos.y())
self.draw_rect_on_image()
def draw_rect_on_image(self):
# 在图片上画出矩形框
pixmap = QPixmap(self.image_path[self.image_index])
painter = QPainter(pixmap)
painter.setPen(QPen(QColor(255, 0, 0), 3, Qt.SolidLine))
painter.drawRect(*self.rect)
self.image_label.setPixmap(pixmap)
def save_folder(self):
# 打开文件夹对话框并选择保存路径
self.save_path = QFileDialog.getExistingDirectory(self, '选择保存文件夹')
def save_image(self):
# 将画了矩形框的图片保存到指定文件夹
if self.save_path and self.rect:
image_name = os.path.basename(self.image_path[self.image_index])
image_path = os.path.join(self.save_path, image_name)
pixmap = QPixmap(self.image_path[self.image_index])
painter = QPainter(pixmap)
painter.setPen(QPen(QColor(255, 0, 0), 3, Qt.SolidLine))
painter.drawRect(*self.rect)
pixmap.save(image_path)
def next_image(self):
# 切换到下一张图片
if self.image_path and self.image_index < len(self.image_path)-1:
self.image_index += 1
self.rect = None
self.load_image()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
```
这个程序通过 PyQt5 实现了一个简单的图像标注工具,可以对指定文件夹中的所有图片进行矩形框标注,并将标注结果保存到指定文件夹中。具体实现细节可以参考代码中的注释。
阅读全文