Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. I can't fit image in Qlabel
Forum Updated to NodeBB v4.3 + New Features

I can't fit image in Qlabel

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hernancrespo89
    wrote on last edited by hernancrespo89
    #1

    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 :)
    1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by
      #2

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

      1 Reply Last reply
      2

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved