我的要求是减小图像的大小并显示在方形框(50 x 50)中。如果图像的大小小于方形框的大小,则图像应按原样显示。作为最初的尝试,我尝试使用以下代码,目的是减少所有图像的大小:
picSize = QtCore.QSize(lbl.width() / 2 , lbl.height() / 2)
但是下面的代码即使在使用后也没有减小图像的大小:
picSize = QtCore.QSize(lbl.width() / 4 , lbl.height() / 4)
请帮我。
import os
import sys
from PySide import QtGui, QtCore
class SecondExample(QtGui.QWidget):
def __init__(self):
super(SecondExample, self).__init__()
self.initUI()
def initUI(self):
self.imgFolder = os.getcwd()
self.widgetLayout = QtGui.QVBoxLayout(self)
self.scrollarea = QtGui.QScrollArea()
self.scrollarea.setWidgetResizable(True)
self.widgetLayout.addWidget(self.scrollarea)
self.widget = QtGui.QWidget()
self.layout = QtGui.QVBoxLayout(self.widget)
self.scrollarea.setWidget(self.widget)
self.layout.setAlignment(QtCore.Qt.AlignHCenter)
for img in os.listdir(self.imgFolder):
imgPath = os.path.join(self.imgFolder, img)
actualImage = QtGui.QImage(imgPath)
pixmap = QtGui.QPixmap(imgPath)
lbl = QtGui.QLabel(self)
lbl.setPixmap(pixmap)
lbl.setScaledContents(True)
picSize = QtCore.QSize(lbl.width() / 2 , lbl.height() / 2)
lbl.resize(picSize)
self.layout.addWidget(lbl)
self.setGeometry(100, 100, 900, 700)
self.setWindowTitle('Viewer')
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = SecondExample()
sys.exit(app.exec_())
if __name__ == '__main__':
main()