酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

用python写一个qt界面,允许用户打开图像文件夹,允许用户在界面选择文件夹里面的上一张图像或下一张图像,并在左侧显示原图像。提供旋转,裁剪图像,放大缩小等处理功能,用户选择图像处理功能后得到的图像结果,并显示在界面右侧。合理布局

```python import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog, QLabel, QPushButton, QVBoxLayout, QHBoxLayout, QWidget, QGraphicsView, QGraphicsScene, QSlider from PyQt5.QtGui import QPixmap, QImageReader, QTransform from PyQt5.QtCore import Qt class ImageProcessor(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setWindowTitle('图像处理器') # 创建布局 main_layout = QHBoxLayout() # 左侧显示原图像 self.image_label = QLabel(self) self.image_label.setAlignment(Qt.AlignCenter) main_layout.addWidget(self.image_label) # 右侧显示处理后的图像 self.processed_image_label = QLabel(self) self.processed_image_label.setAlignment(Qt.AlignCenter) main_layout.addWidget(self.processed_image_label) # 创建按钮和滑块 button_layout = QVBoxLayout() self.open_button = QPushButton('打开文件夹', self) self.open_button.clicked.connect(self.open_folder) button_layout.addWidget(self.open_button) self.prev_button = QPushButton('上一张', self) self.prev_button.clicked.connect(self.show_previous_image) button_layout.addWidget(self.prev_button) self.next_button = QPushButton('下一张', self) self.next_button.clicked.connect(self.show_next_image) button_layout.addWidget(self.next_button) self.rotate_slider = QSlider(Qt.Horizontal, self) self.rotate_slider.setRange(0, 360) self.rotate_slider.valueChanged.connect(self.rotate_image) button_layout.addWidget(self.rotate_slider) self.zoom_slider = QSlider(Qt.Horizontal, self) self.zoom_slider.setRange(1, 200) self.zoom_slider.valueChanged.connect(self.zoom_image) button_layout.addWidget(self.zoom_slider) main_layout.addLayout(button_layout) central_widget = QWidget() central_widget.setLayout(main_layout) self.setCentralWidget(central_widget) self.image_files = [] self.current_index = -1 def open_folder(self): folder_path = QFileDialog.getExistingDirectory(self, '选择文件夹') if folder_path: self.image_files = [f for f in os.listdir(folder_path) if f.endswith(('.png', '.jpg', '.jpeg'))] self.current_index = 0 self.show_image() def show_image(self): if self.current_index >= 0 and self.current_index < len(self.image_files): image_path = self.image_files[self.current_index] pixmap = QPixmap(image_path) self.image_label.setPixmap(pixmap) self.processed_image_label.clear() def show_previous_image(self): if self.current_index > 0: self.current_index -= 1 self.show_image() def show_next_image(self): if self.current_index < len(self.image_files) - 1: self.current_index += 1 self.show_image() def rotate_image(self, angle): if self.current_index >= 0 and self.current_index < len(self.image_files): image_path = self.image_files[self.current_index] image = QImageReader(image_path).read() transform = QTransform().rotate(angle) rotated_image = image.transformed(transform) pixmap = QPixmap.fromImage(rotated_image) self.processed_image_label.setPixmap(pixmap) def zoom_image(self, value): if self.current_index >= 0 and self.current_index < len(self.image_files): image_path = self.image_files[self.current_index] image = QImageReader(image_path).read() transform = QTransform().scale(value / 100, value / 100) zoomed_image = image.transformed(transform) pixmap = QPixmap.fromImage(zoomed_image) self.processed_image_label.setPixmap(pixmap) if __name__ == '__main__': app = QApplication(sys.argv) window = ImageProcessor() window.show() sys.exit(app.exec_()) ``` [2024-07-28 09:41:52 | AI写代码神器 | 1327点数解答]

相关提问