Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

I can't fit image in Qlabel



  • Hi i am new with PyQt and i am using QtDesigner and Python.
    i have been trying to display a image on Qt. I acquire image from webcam and it's size is 640x480. But i don't want display it on full size. I want to display it on a label. Size of label is not important. When i display it on a label. It crops the image and it shows some pixels (as expected). But i want to fit image in Qlabel. So i tried to set QSizePolicy=Ignored and QSizePolicy=Fixed using QtDesigner but bot of them didn't work. How can i display image like i said?

    my codes:

    main.py

    from PyQt5.QtWidgets import *
    
    from qt_ilk import loadUi_example
    
    
    if __name__ == '__main__': 
    
        app = QApplication([])
        start_window = loadUi_example()
        start_window.show()
        app.exit(app.exec_())
    
    

    and qt_ilk.py

    # -*- coding: utf-8 -*-
    
    # Form implementation generated from reading ui file 'qt_ilk.ui'
    #
    # Created by: PyQt5 UI code generator 5.6
    #
    # WARNING! All changes made in this file will be lost!
    from PyQt5.QtWidgets import *
    from PyQt5.uic import loadUi
    from PyQt5 import QtCore, QtGui, QtWidgets
    from PyQt5.QtCore import pyqtSlot
    from PyQt5.QtGui import QPixmap, QImage
    import cv2
    
    class loadUi_example(QMainWindow):
    
        def __init__(self):
            super().__init__()
            loadUi("qt_ilk.ui", self)
    
        @pyqtSlot()
        def on_buon2_clicked(self):
            cap = cv2.VideoCapture(0)
    
            ret, frame = cap.read()
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            if frame is not None:
                image = QImage(frame, frame.shape[1], frame.shape[0],
                               QImage.Format_RGB888)  # The image is stored using a 24-bit RGB format (8-8-8).
    
                self.pixmap = QPixmap.fromImage(image)
            self.label.setPixmap(self.pixmap)
            cap.release()
    
            cv2.destroyAllWindows()
    
    class Ui_Form(object):
        def setupUi(self, Form):
            Form.setObjectName("Form")
            Form.resize(1000, 1000)
            self.buon2 = QtWidgets.QPushButton(Form)
            self.buon2.setGeometry(QtCore.QRect(150, 200, 75, 23))
            self.buon2.setObjectName("buon2")
            self.label = QtWidgets.QLabel(Form)
            self.label.setGeometry(QtCore.QRect(170, 70, 47, 13))
            self.label.setText("")
            self.label.setObjectName("label")
            self.buton1 = QtWidgets.QPushButton(Form)
            self.buton1.setGeometry(QtCore.QRect(150, 160, 75, 23))
            self.buton1.setObjectName("buton1")
    
            self.retranslateUi(Form)
            QtCore.QMetaObject.connectSlotsByName(Form)
    
        def retranslateUi(self, Form):
            _translate = QtCore.QCoreApplication.translate
            Form.setWindowTitle(_translate("Form", "Form"))
            self.buon2.setText(_translate("Form", "goster"))
            self.buton1.setText(_translate("Form", "temizle"))
    
    
    
    
    ```![Untitled.png](https://ddgobkiprc33d.cloudfront.net/3542ad13-119b-4ea5-9d20-d75557e468fc.png) 
    
    There is my face on original frame :)


  • Is QLabel.setScaledContents(true) what you are looking for?



  • Is QLabel.setScaledContents(true) what you are looking for?


Log in to reply