Python+PyQt5:打造多功能图像查看与处理界面,支持选图、旋转裁剪缩放!
为了实现这个需求,我们可以使用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_() ``` 这段代码创建了一个简单的图像查看器和编辑器,支持基本的功能如选择文件夹、浏览图片、旋转、裁剪和缩放等操作。你可以根据需要进一步扩展功能或改进用户界面。 ######[AI写代码神器 | 1186点数解答 | 2024-07-28 09:42:17]
- Java JFrame 实现多界面跳转:含登录、注册及个人信息管理功能(讯飞星火 | 503点数解答 | 2024-06-06 23:49:29)408
- Python 运行报错:urllib3 网络连接创建失败,原因及排查建议(字节豆包 | 161点数解答 | 2024-10-31 00:39:36)489
- Python 实战:学生信息列表转字典、遍历、深拷贝及数据修改全流程(GPT | 422点数解答 | 2024-10-29 15:43:54)441
- Python实战:学生成绩列表转字典,深拷贝与数据修改操作全解析(字节豆包 | 254点数解答 | 2024-10-29 16:01:39)435
- CentOS下Java进程出现futex_wait_queue_me问题的处理全流程(阿里通义 | 283点数解答 | 2023-11-20 14:51:26)483
- 替雷利珠单抗致免疫相关性糖尿病:病例报道与机制探讨,为临床用药敲响警钟( | 164点数解答 | 2024-10-11 14:52:52)285
- Python 代码转 C++:计算花朵鲜艳值总和(字节豆包 | 408点数解答 | 2026-04-19 21:50:57)17
- 用 C++ 和 OpenCV 生成带随机偏移的圆周上的点:代码剖析(字节豆包 | 29点数解答 | 2024-11-06 23:40:42)271
- Java调用Python接口中文乱码?设置UTF - 8编码一招解决!(讯飞星火 | 263点数解答 | 2024-06-06 17:07:59)414
- 解决Java调用Python接口中文乱码问题:设置UTF - 8编码全攻略(讯飞星火 | 160点数解答 | 2024-06-06 17:18:39)440
- Java调用Python接口中文乱码问题:字符编码统一解决方案(讯飞星火 | 344点数解答 | 2024-06-06 17:19:55)546
- 解决Java调用Python接口时中文值乱码问题:设置字符编码为UTF-8(讯飞星火 | 264点数解答 | 2024-06-06 17:27:09)412