1

I have a window that has six symmetrically placed labels, all showing images (designed using qt-designer with the help of layouts). I would like to resize these images according to the changing window size. I have found some help in previous questions like: PyQt: Detect resizing in Widget-window resized signal

At present, using resizeEvent() in my case does not shrink the images according to the resize function. It is already triggered with the display of my form window thereby making the pushButton useless. Above all, the resulting execution is very slow. My images are of 2058x1536 dimension and displayed transparently.

My qt-designer code is given here: https://pastebin.com/TzM6qiKZ

import Ui_ImageCrop_Test
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5.QtGui import QPixmap, QImage, QPainter, QColor
from PyQt5.QtCore import Qt

class ImageCrop(Ui_ImageCrop_Test.Ui_MainWindow, QMainWindow):
    def __init__(self, parent=None):
        super(ImageCrop, self).__init__()
        self.setupUi(self)
        self.transparency = 220

        with open("Img_files.txt") as file:
            self.img_files = file.read().splitlines()
        self.length = len(self.img_files)

        self.pushButton_1.clicked.connect(self.click1)
        self.label_1.resizeEvent = self.click1

    def click1(self, event):
        for i in range(6):
            image = QImage(self.img_files[i])
            image = image.convertToFormat(QImage.Format_ARGB8565_Premultiplied)

            p = QPainter(image)
            p.setCompositionMode(QPainter.CompositionMode_DestinationIn)
            p.fillRect(image.rect(), QColor(0, 0, 0, self.transparency))
            p.end()

            pixmap = QPixmap(image)
            w = int(self.label_1.width() - 4.0)
            h = int(self.label_1.height() - 4.0)
            smaller_pixmap = pixmap.scaled(w, h, Qt.IgnoreAspectRatio, Qt.FastTransformation)

            if i == 0:
                self.label_1.setPixmap(smaller_pixmap)

            if i == 1:
                self.label_2.setPixmap(smaller_pixmap)

            if i == 2:
                self.label_3.setPixmap(smaller_pixmap)

            if i == 3:
                self.label_4.setPixmap(smaller_pixmap)

            if i == 4:
                self.label_5.setPixmap(smaller_pixmap)

            if i == 5:
                self.label_6.setPixmap(smaller_pixmap)


def main():
    app = QApplication(sys.argv)
    form1 = ImageCrop()
    form1.show()
    app.exec_()


if __name__ == '__main__': main()

Is there any solution to run this code faster? For example, I was thinking to make all my labels turn blank during a mouse click at the edge of my window and then images reappear after the mouse button is released. This does not seem so neat. Also, I am not sure if using paintEvent can reduce my lag. Thank you for your suggestions and comments.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
ABK
  • 145
  • 1
  • 14

1 Answers1

0

QLabel has the scaledContents property that allows the image to scale automatically:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
import Ui_ImageCrop_Test

class ImageCrop(QtWidgets.QMainWindow, Ui_ImageCrop_Test.Ui_MainWindow):
    def __init__(self, parent=None):
        super(ImageCrop, self).__init__()
        self.setupUi(self)
        self.pushButton_1.clicked.connect(self.click1)
        self.transparency = 220
        with open("Img_files.txt") as file:
            self.img_files = file.read().splitlines()

    @QtCore.pyqtSlot()
    def click1(self):
        labels = [self.label_1, self.label_2, self.label_3, 
            self.label_4, self.label_5, self.label_6]
        for label, filename in zip(labels, self.img_files):
            image = QtGui.QImage(filename)
            image = image.convertToFormat(QtGui.QImage.Format_ARGB8565_Premultiplied)

            p = QtGui.QPainter(image)
            p.setCompositionMode(QtGui.QPainter.CompositionMode_DestinationIn)
            p.fillRect(image.rect(), QtGui.QColor(0, 0, 0, self.transparency))
            p.end()
            pixmap = QtGui.QPixmap(image)
            w = int(label.width() - 4.0)
            h = int(label.height() - 4.0)
            smaller_pixmap = pixmap.scaled(w, h, QtCore.Qt.IgnoreAspectRatio, QtCore.Qt.FastTransformation)
            label.setPixmap(smaller_pixmap)
            label.setScaledContents(True)

def main():
    app = QtWidgets.QApplication(sys.argv)
    form1 = ImageCrop()
    form1.show()
    app.exec_()

if __name__ == '__main__': main()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • label.setScaledContents(True) is the solution. Also, making those labels into a list made my code shorter. – ABK Feb 20 '19 at 09:00