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

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

为了实现这个需求,我们可以使用Python中的`PyQt5`库来构建GUI,并使用`PIL`(Python Imaging Library)库来进行图像处理。 首先确保你安装了所需的库: ```bash pip install PyQt5 pip install pillow ``` 接下来是具体的代码实现: ```python import os from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QHBoxLayout, QLabel, QFileDialog, QSlider, QComboBox, QSpinBox from PyQt5.QtGui import QPixmap, QImage from PyQt5.QtCore import Qt from PIL import Image, ImageOps import numpy as np class ImageProcessor(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): # 创建控件 self.image_label_left = QLabel(self) self.image_label_right = QLabel(self) self.prev_button = QPushButton('Previous', self) self.next_button = QPushButton('Next', self) self.open_folder_button = QPushButton('Open Folder', self) self.rotation_slider = QSlider(Qt.Horizontal) self.crop_x_spinbox = QSpinBox() self.crop_y_spinbox = QSpinBox() self.crop_width_spinbox = QSpinBox() self.crop_height_spinbox = QSpinBox() self.zoom_slider = QSlider(Qt.Horizontal) # 初始化状态 self.current_image_index = 0 self.image_files = [] self.folder_path = None # 布局设置 main_layout = QVBoxLayout(self) image_layout = QHBoxLayout() image_layout.addWidget(self.image_label_left) image_layout.addWidget(self.image_label_right) button_layout = QHBoxLayout() button_layout.addWidget(self.prev_button) button_layout.addWidget(self.next_button) button_layout.addWidget(self.open_folder_button) crop_layout = QHBoxLayout() crop_layout.addWidget(QLabel("X:")) crop_layout.addWidget(self.crop_x_spinbox) crop_layout.addWidget(QLabel("Y:")) crop_layout.addWidget(self.crop_y_spinbox) crop_layout.addWidget(QLabel("Width:")) crop_layout.addWidget(self.crop_width_spinbox) crop_layout.addWidget(QLabel("Height:")) crop_layout.addWidget(self.crop_height_spinbox) slider_layout = QHBoxLayout() slider_layout.addWidget(QLabel("Rotation:")) slider_layout.addWidget(self.rotation_slider) slider_layout.addWidget(QLabel("Zoom:")) slider_layout.addWidget(self.zoom_slider) main_layout.addLayout(image_layout) main_layout.addLayout(button_layout) main_layout.addLayout(crop_layout) main_layout.addLayout(slider_layout) # 设置信号与槽 self.open_folder_button.clicked.connect(self.openFolder) self.prev_button.clicked.connect(self.showPrevImage) self.next_button.clicked.connect(self.showNextImage) self.rotation_slider.valueChanged.connect(self.updateImage) self.crop_x_spinbox.valueChanged.connect(self.updateImage) self.crop_y_spinbox.valueChanged.connect(self.updateImage) self.crop_width_spinbox.valueChanged.connect(self.updateImage) self.crop_height_spinbox.valueChanged.connect(self.updateImage) self.zoom_slider.valueChanged.connect(self.updateImage) def openFolder(self): self.folder_path = QFileDialog.getExistingDirectory(self, "Select Directory") if self.folder_path: self.image_files = [os.path.join(self.folder_path, f) for f in os.listdir(self.folder_path) if f.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp'))] if len(self.image_files) > 0: self.showImage(self.image_files[self.current_image_index]) def showImage(self, image_path): pixmap = QPixmap(image_path) self.image_label_left.setPixmap(pixmap) self.updateImage() def showPrevImage(self): if len(self.image_files) > 0: self.current_image_index = (self.current_image_index - 1) % len(self.image_files) self.showImage(self.image_files[self.current_image_index]) def showNextImage(self): if len(self.image_files) > 0: self.current_image_index = (self.current_image_index + 1) % len(self.image_files) self.showImage(self.image_files[self.current_image_index]) def updateImage(self): if len(self.image_files) > 0: image_path = self.image_files[self.current_image_index] # 加载图像 image = Image.open(image_path) # 裁剪 x = self.crop_x_spinbox.value() y = self.crop_y_spinbox.value() width = self.crop_width_spinbox.value() height = self.crop_height_spinbox.value() image = image.crop((x, y, x + width, y + height)) # 旋转 angle = self.rotation_slider.value() image = image.rotate(angle, expand=True) # 缩放 zoom = self.zoom_slider.value() / 100.0 size = (int(image.width * zoom), int(image.height * zoom)) image = image.resize(size, Image.ANTIALIAS) # 显示处理后的图像 qimage = QImage(np.array(image).data, image.width, image.height, QImage.Format_RGB888) pixmap = QPixmap.fromImage(qimage.rgbSwapped()) self.image_label_right.setPixmap(pixmap) if __name__ == '__main__': app = QApplication([]) ex = ImageProcessor() ex.show() app.exec_() ``` 这段代码创建了一个简单的图像查看器和编辑器,支持基本的功能如选择文件夹、浏览图片、旋转、裁剪和缩放等操作。你可以根据需要进一步扩展功能或改进用户界面。 [2024-07-28 09:42:17 | AI写代码神器 | 1186点数解答]

相关提问