用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_()) ``` ######[AI写代码神器 | 1327点数解答 | 2024-07-28 09:41:52]
- 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
- Qt 绘图全解析:从原理到示例及关键逻辑 (字节豆包 | 265点数解答 | 2025-03-12 10:55:54)188
- Qt 实战:用 QGraphics 图元轻松实现绘图(附示例代码)(字节豆包 | 226点数解答 | 2025-03-12 10:56:56)220
- 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
- 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