尝试修复zoom in无效

This commit is contained in:
lyz55 2024-12-19 14:18:12 +08:00
parent b9c7892fb6
commit 28ec20e52c

View File

@ -5,6 +5,15 @@ import numpy as np
from PyQt5 import QtCore, QtGui, QtWidgets
class ImageLabel(QtWidgets.QLabel):
def __init__(self, parent=None):
super().__init__(parent)
self.setAlignment(QtCore.Qt.AlignCenter)
self.setBackgroundRole(QtGui.QPalette.Base)
self.setSizePolicy(QtWidgets.QSizePolicy.Ignored, QtWidgets.QSizePolicy.Ignored)
self.setScaledContents(True)
class ImageViewer(QtWidgets.QWidget):
def __init__(self, folder_path):
super().__init__()
@ -14,6 +23,7 @@ class ImageViewer(QtWidgets.QWidget):
self.current_index = 0
self.threshold_g = 50 # 初始 T_g 值,根据需要调整
self.threshold_diff = 10 # 初始 T 值,根据需要调整
self.zoom_factor = 1.0 # 初始缩放因子
# 设置UI
self.initUI()
@ -33,14 +43,22 @@ class ImageViewer(QtWidgets.QWidget):
# 右侧区域
right_layout = QtWidgets.QVBoxLayout()
# 图片显示区域
self.original_label = QtWidgets.QLabel("Original Image")
self.masked_label = QtWidgets.QLabel("Masked Image")
# 图片显示区域使用 QScrollArea 以支持滚动
self.original_label = ImageLabel("Original Image")
self.masked_label = ImageLabel("Masked Image")
self.original_scroll = QtWidgets.QScrollArea()
self.original_scroll.setWidgetResizable(True)
self.original_scroll.setWidget(self.original_label)
self.masked_scroll = QtWidgets.QScrollArea()
self.masked_scroll.setWidgetResizable(True)
self.masked_scroll.setWidget(self.masked_label)
# 使用垂直布局上下显示两张图片
images_layout = QtWidgets.QVBoxLayout()
images_layout.addWidget(self.original_label)
images_layout.addWidget(self.masked_label)
images_layout.addWidget(self.original_scroll)
images_layout.addWidget(self.masked_scroll)
right_layout.addLayout(images_layout, 8)
# 阈值调整区域
@ -76,20 +94,29 @@ class ImageViewer(QtWidgets.QWidget):
right_layout.addLayout(threshold_layout, 2)
# 按钮
# 按钮区域(包括导航和缩放按钮)
button_layout = QtWidgets.QHBoxLayout()
self.prev_button = QtWidgets.QPushButton("Previous (P)")
self.next_button = QtWidgets.QPushButton("Next (N)")
self.zoom_in_button = QtWidgets.QPushButton("Zoom In (+)")
self.zoom_out_button = QtWidgets.QPushButton("Zoom Out (-)")
self.prev_button.clicked.connect(self.showPreviousImage)
self.next_button.clicked.connect(self.showNextImage)
self.zoom_in_button.clicked.connect(self.zoomIn)
self.zoom_out_button.clicked.connect(self.zoomOut)
button_layout.addWidget(self.prev_button)
button_layout.addWidget(self.next_button)
button_layout.addStretch()
button_layout.addWidget(self.zoom_in_button)
button_layout.addWidget(self.zoom_out_button)
right_layout.addLayout(button_layout, 1)
main_layout.addLayout(right_layout, 4)
self.setLayout(main_layout)
self.setWindowTitle("BMP Image Viewer with Mask")
self.setWindowTitle("BMP Image Viewer with Mask and Zoom")
self.resize(1600, 900)
def loadImage(self, index):
@ -113,6 +140,9 @@ class ImageViewer(QtWidgets.QWidget):
self.masked_image = masked_image
self.displayImage(self.masked_label, self.masked_image)
# 重置缩放因子
self.zoom_factor = 1.0
# 更新侧边栏选择
self.sidebar.blockSignals(True)
self.sidebar.setCurrentRow(index)
@ -132,8 +162,10 @@ class ImageViewer(QtWidgets.QWidget):
bytes_per_line = 3 * width
q_image = QtGui.QImage(rgb_image.data, width, height, bytes_per_line, QtGui.QImage.Format_RGB888)
pixmap = QtGui.QPixmap.fromImage(q_image)
label.setPixmap(pixmap)
label.setAlignment(QtCore.Qt.AlignCenter)
scaled_pixmap = pixmap.scaled(pixmap.size() * self.zoom_factor, QtCore.Qt.KeepAspectRatio,
QtCore.Qt.SmoothTransformation)
label.setPixmap(scaled_pixmap)
label.adjustSize()
def generateMask(self, image, threshold_g, threshold_diff):
# 分离 G 和 R 通道
@ -217,9 +249,27 @@ class ImageViewer(QtWidgets.QWidget):
self.showNextImage()
elif event.key() == QtCore.Qt.Key_P:
self.showPreviousImage()
elif event.key() == QtCore.Qt.Key_Plus or event.key() == QtCore.Qt.Key_Equal:
self.zoomIn()
elif event.key() == QtCore.Qt.Key_Minus:
self.zoomOut()
else:
super().keyPressEvent(event)
def zoomIn(self):
self.zoom_factor *= 1.25
self.updateDisplayImages()
def zoomOut(self):
self.zoom_factor /= 1.25
self.updateDisplayImages()
def updateDisplayImages(self):
if hasattr(self, 'original_image'):
self.displayImage(self.original_label, self.original_image)
if hasattr(self, 'masked_image'):
self.displayImage(self.masked_label, self.masked_image)
def main():
app = QtWidgets.QApplication(sys.argv)