import sysfrom PyQt5.QtWidgets import QApplication, QMainWindow, QCheckBox, QScrollArea, QSizePolicy, QFileDialog, QLabel, QPushButton, QVBoxLayout, QWidget, QHBoxLayout, QSlider, QLineEditfrom PyQt5.QtGui import QPixmap, QImage, QColorfrom PyQt5.QtCore import Qt, QSizeclass MainWindow(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setGeometry(100, 100, 800, 600) self.setWindowTitle('Image Loader') # 创建导入按钮 self.loadButton = QPushButton('导入', self) self.loadButton.clicked.connect(self.loadImage) # 创建自适应复选框 self.adaptiveBox = QCheckBox("自适应", self) self.adaptiveBox.stateChanged.connect(self.toggleAdaptive) # 创建放大和缩小按钮 self.zoomInButton = QPushButton("放大", self) self.zoomInButton.clicked.connect(self.zoomIn) self.zoomOutButton = QPushButton("缩小", self) self.zoomOutButton.clicked.connect(self.zoomOut) # 创建色相滑块和输入框 self.hueSlider = QSlider(Qt.Horizontal, self) self.hueSlider.setRange(0, 360) self.hueSlider.setValue(0) self.hueSlider.valueChanged.connect(self.changeHue) self.hueLineEdit = QLineEdit(str(0), self) self.hueLineEdit.editingFinished.connect(self.changeHueFromLineEdit) # 创建预览复选框 self.previewCheckBox = QCheckBox("预览", self) self.previewCheckBox.stateChanged.connect(self.togglePreview) # 创建图片显示标签和滚动区域 self.imageLabel = QLabel(self) self.imageLabel.setAlignment(Qt.AlignLeft | Qt.AlignTop) self.imageLabel.setMinimumSize(1, 1) self.scrollArea = QScrollArea(self) self.scrollArea.setWidget(self.imageLabel) self.scrollArea.setAlignment(Qt.AlignLeft | Qt.AlignTop) self.setCentralWidget(self.scrollArea) # 设置布局 buttonLayout = QHBoxLayout() buttonLayout.addWidget(self.loadButton) buttonLayout.addWidget(self.adaptiveBox) buttonLayout.addWidget(self.zoomInButton) buttonLayout.addWidget(self.zoomOutButton) buttonLayout.addWidget(self.hueSlider) buttonLayout.addWidget(self.hueLineEdit) buttonLayout.addWidget(self.previewCheckBox) buttonWidget = QWidget(self) buttonWidget.setLayout(buttonLayout) self.addToolBar(Qt.TopToolBarArea, buttonWidget) self.setMinimumSize(320, 240) self.setMaximumSize(1920, 1080) self.resize(640, 480) # 初始化变量 self.image = None self.previewImage = None self.preview = False self.adaptive = False self.zoomFactor = 1.0 def loadImage(self): file_name, _ = QFileDialog.getOpenFileName(None, '打开图片文件', '.', '图像文件(*.jpg *.jpeg *.png *.bmp *.gif)') if file_name: self.image = QPixmap(file_name) self.showImage() def showImage(self): if self.image: if self.adaptive: self.imageLabel.setPixmap(self.image.scaled(self.scrollArea.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation)) else: self.imageLabel.setPixmap(self.image.scaled(self.image.size() * self.zoomFactor, Qt.KeepAspectRatio, Qt.SmoothTransformation)) self.scrollArea.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded) self.scrollArea.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded) self.scrollArea.setWidgetResizable(True) def toggleAdaptive(self, state): if state == Qt.Checked: self.adaptive = True else: self.adaptive = False self.showImage() def zoomIn(self): self.zoomFactor *= 1.1 self.showImage() def zoomOut(self): self.zoomFactor /= 1.1 self.showImage() def changeHue(self, value): if self.image: if not self.preview: self.previewImage = self.image.copy() hue = QColor.fromHsv(value, 255, 255) image = self.previewImage.toImage() for i in range(image.width()): for j in range(image.height()): c = QColor(image.pixel(i, j)) c.setHsv(hue.hue(), c.saturation(), c.value()) image.setPixelColor(i, j, c) self.imageLabel.setPixmap(QPixmap.fromImage(image)) def changeHueFromLineEdit(self): try: value = int(self.hueLineEdit.text()) if value < 0: value = 0 elif value > 360: value = 360 self.hueSlider.setValue(value) except: pass def togglePreview(self, state): if state == Qt.Checked: self.preview = True self.previewImage = self.image.copy() self.changeHue(self.hueSlider.value()) else: self.preview = False self.showImage()if __name__ == '__main__': app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec_())
这个代码框架可以实现大部分的功能,你可以根据自己的需求来逐一完善。其中的一些细节问题还需要你自己去调试和解决,祝你好运!