컴퓨터/Python

python pyqt5 이미지 창크기에 맞추어 표시

풍경소리^^ 2022. 6. 14. 09:02
import sys
from turtle import width
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QFileDialog, QPushButton
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtCore import pyqtSlot
#import TurkceOcr as ocr
class App(QWidget):
    def __init__(self):
        super().__init__()
        self.title = '그림파일 열기'
        self.left = 50
        self.top = 50
        self.width = 320
        self.height = 360
        self.initUI()
    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        # Create widget
        button = QPushButton('그림파일선택', self)
        button.setToolTip('This is load picture button')
        button.move(10, 10)
        button.clicked.connect(self.on_click)
        self.label = QLabel(self)
        self.label.move(10,50)
        #self.resize(pixmap.width(), pixmap.height())
        self.show()
    @pyqtSlot()
    def on_click(self):
        print('PyQt5 button click')
        image = QFileDialog.getOpenFileName(None, 'OpenFile', '', "Image file(*.jpg *.png)")
        imagePath = image[0]
        pixmap = QPixmap(imagePath)
        # self.resize(pixmap.width(), pixmap.height())
        # print(pixmap.size()) 
        # print(pixmap.width()) 
        # print(pixmap.height()) 
        width = 300
        height = 300
        if pixmap.width() >= pixmap.height():
            self.label.setPixmap(pixmap.scaledToWidth(width))
        else:
            self.label.setPixmap(pixmap.scaledToHeight(height))
        # self.label.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)
        self.label.adjustSize()                       # <---
        # self.label.setScaledContents(True)
        #print(ocr.resimden_yaziya(imagePath))
        print(imagePath)
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())