如何在pyqt5上用QPixmap实现图片缩放
How to zoom a picture implemented with QPixmap on pyqt5
我正在尝试放大我用 QPixmap 实现的图片,但没有任何效果。这是我实现这个的方式。 (self.hauteur是代表身高的浮点数)
self.label.setPixmap(QPixmap(self.image).scaledToHeight(self.hauteur))
我已经将我的标签放入布局中,以便将其放入滚动小部件中。
self.layout6.addWidget(self.label)
...
self.widget1=QWidget()
self.widget1.setLayout(self.layout6)
self.scroll1.setWidget(self.widget1)
self.tab1.layout.addWidget(self.scroll1,0,1,1,2)
...
self.tab1.setLayout(self.tab1.layout)
如果我想构建一个通过按下按钮来放大或缩小我 window 中的图片的方法,我该怎么办?我试过调整标签的大小,但没有用。
self.action5=QAction(QIcon("Icon/in"),"Zoom in",self)
self.action5.triggered.connect(self.zoomin)
...
def zoomin(self):
self.hauteur+=100
self.label.scaledToHeight(self.hauteur)
self.update()
我会调整原始 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()
我正在尝试放大我用 QPixmap 实现的图片,但没有任何效果。这是我实现这个的方式。 (self.hauteur是代表身高的浮点数)
self.label.setPixmap(QPixmap(self.image).scaledToHeight(self.hauteur))
我已经将我的标签放入布局中,以便将其放入滚动小部件中。
self.layout6.addWidget(self.label)
...
self.widget1=QWidget()
self.widget1.setLayout(self.layout6)
self.scroll1.setWidget(self.widget1)
self.tab1.layout.addWidget(self.scroll1,0,1,1,2)
...
self.tab1.setLayout(self.tab1.layout)
如果我想构建一个通过按下按钮来放大或缩小我 window 中的图片的方法,我该怎么办?我试过调整标签的大小,但没有用。
self.action5=QAction(QIcon("Icon/in"),"Zoom in",self)
self.action5.triggered.connect(self.zoomin)
...
def zoomin(self):
self.hauteur+=100
self.label.scaledToHeight(self.hauteur)
self.update()
我会调整原始 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()