Python 如何在pyqt5上使用QPixmap实现图片缩放
我试图放大我用QPixmap实现的图片,但没有任何效果。下面是我实现这一点的方法。(self.auteur是表示高度的浮点数) 我已经把我的标签放到一个布局中,以便把它放到一个滚动小部件中Python 如何在pyqt5上使用QPixmap实现图片缩放,python,pyqt5,qlabel,qpixmap,Python,Pyqt5,Qlabel,Qpixmap,我试图放大我用QPixmap实现的图片,但没有任何效果。下面是我实现这一点的方法。(self.auteur是表示高度的浮点数) 我已经把我的标签放到一个布局中,以便把它放到一个滚动小部件中 self.layout6.addWidget(self.label) 如果我想建立一种方法,通过按下按钮放大或缩小窗口中的图片,我该怎么做?我试图调整标签的大小,但没有效果 self.action5=QAction(QIcon("Icon/in"),"Zoom in",self) self.ac
self.layout6.addWidget(self.label)
如果我想建立一种方法,通过按下按钮放大或缩小窗口中的图片,我该怎么做?我试图调整标签的大小,但没有效果
self.action5=QAction(QIcon("Icon/in"),"Zoom in",self)
self.action5.triggered.connect(self.zoomin)
我会调整原始的
QPixmap
大小,并将其再次放入label
self.scale *= 2
size = self.pixmap.size()
scaled_pixmap = self.pixmap.scaled(self.scale * size)
self.label.setPixmap(scaled_pixmap)
最低工作代码:
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class MyApp(QWidget):
def __init__(self, *args):
super().__init__(*args)
self.layout = QVBoxLayout()
self.setLayout(self.layout)
self.button_zoom_in = QPushButton('Zoom IN', self)
self.button_zoom_in.clicked.connect(self.on_zoom_in)
self.layout.addWidget(self.button_zoom_in)
self.button_zoom_out = QPushButton('Zoom OUT', self)
self.button_zoom_out.clicked.connect(self.on_zoom_out)
self.layout.addWidget(self.button_zoom_out)
self.pixmap = QPixmap('image.jpg')
self.label = QLabel(self)
self.label.setPixmap(self.pixmap)
self.layout.addWidget(self.label)
self.scale = 1
self.show()
def on_zoom_in(self, event):
self.scale *= 2
self.resize_image()
def on_zoom_out(self, event):
self.scale /= 2
self.resize_image()
def resize_image(self):
size = self.pixmap.size()
scaled_pixmap = self.pixmap.scaled(self.scale * size)
self.label.setPixmap(scaled_pixmap)
# --- main ---
app = QApplication([])
win = MyApp()
app.exec()
编辑:使用
self.height+=100
而不是self.scale*=2
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class MyApp(QWidget):
def __init__(self, *args):
super().__init__(*args)
self.layout = QVBoxLayout()
self.setLayout(self.layout)
self.button_zoom_in = QPushButton('Zoom IN', self)
self.button_zoom_in.clicked.connect(self.on_zoom_in)
self.layout.addWidget(self.button_zoom_in)
self.button_zoom_out = QPushButton('Zoom OUT', self)
self.button_zoom_out.clicked.connect(self.on_zoom_out)
self.layout.addWidget(self.button_zoom_out)
self.pixmap = QPixmap('image.jpg')
self.height = self.pixmap.height()
self.label = QLabel(self)
self.label.setPixmap(self.pixmap)
self.layout.addWidget(self.label)
self.show()
def on_zoom_in(self, event):
self.height += 100
self.resize_image()
def on_zoom_out(self, event):
self.height -= 100
self.resize_image()
def resize_image(self):
scaled_pixmap = self.pixmap.scaledToHeight(self.height)
self.label.setPixmap(scaled_pixmap)
# --- main ---
app = QApplication([])
win = MyApp()
app.exec()
请提供一个您提供的代码不是MRE,请阅读您尝试缩放标签的链接,但可能您必须缩放原始图像
QPixmap
,然后再次将其放入标签中(以替换以前的图像)。
def zoomin(self):
self.hauteur+=100
self.label.scaledToHeight(self.hauteur)
self.update()
self.scale *= 2
size = self.pixmap.size()
scaled_pixmap = self.pixmap.scaled(self.scale * size)
self.label.setPixmap(scaled_pixmap)
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class MyApp(QWidget):
def __init__(self, *args):
super().__init__(*args)
self.layout = QVBoxLayout()
self.setLayout(self.layout)
self.button_zoom_in = QPushButton('Zoom IN', self)
self.button_zoom_in.clicked.connect(self.on_zoom_in)
self.layout.addWidget(self.button_zoom_in)
self.button_zoom_out = QPushButton('Zoom OUT', self)
self.button_zoom_out.clicked.connect(self.on_zoom_out)
self.layout.addWidget(self.button_zoom_out)
self.pixmap = QPixmap('image.jpg')
self.label = QLabel(self)
self.label.setPixmap(self.pixmap)
self.layout.addWidget(self.label)
self.scale = 1
self.show()
def on_zoom_in(self, event):
self.scale *= 2
self.resize_image()
def on_zoom_out(self, event):
self.scale /= 2
self.resize_image()
def resize_image(self):
size = self.pixmap.size()
scaled_pixmap = self.pixmap.scaled(self.scale * size)
self.label.setPixmap(scaled_pixmap)
# --- main ---
app = QApplication([])
win = MyApp()
app.exec()
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class MyApp(QWidget):
def __init__(self, *args):
super().__init__(*args)
self.layout = QVBoxLayout()
self.setLayout(self.layout)
self.button_zoom_in = QPushButton('Zoom IN', self)
self.button_zoom_in.clicked.connect(self.on_zoom_in)
self.layout.addWidget(self.button_zoom_in)
self.button_zoom_out = QPushButton('Zoom OUT', self)
self.button_zoom_out.clicked.connect(self.on_zoom_out)
self.layout.addWidget(self.button_zoom_out)
self.pixmap = QPixmap('image.jpg')
self.height = self.pixmap.height()
self.label = QLabel(self)
self.label.setPixmap(self.pixmap)
self.layout.addWidget(self.label)
self.show()
def on_zoom_in(self, event):
self.height += 100
self.resize_image()
def on_zoom_out(self, event):
self.height -= 100
self.resize_image()
def resize_image(self):
scaled_pixmap = self.pixmap.scaledToHeight(self.height)
self.label.setPixmap(scaled_pixmap)
# --- main ---
app = QApplication([])
win = MyApp()
app.exec()