PyQt5调整显示图片大小适合窗口

PyQt5调整显示图片大小适合窗口

话不多说,详见代码

注意修改路径

import sys
import cv2
import numpy as np
from PIL import Image
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class Window(QWidget):

   def __init__(self,img_dir):
       super(Window, self).__init__()

       self.init_img = cv2.imread(img_dir)
       self.resize(800,800)
       self.setWindowTitle("DynamicWindow")
       self.font = cv2.FONT_HERSHEY_SIMPLEX
       # set your own background
       palette = QPalette()
       palette.setBrush(QPalette.Background, QBrush(QPixmap('./icon/backend.jpg')))
       self.setPalette(palette)
       # image window
       self.label1 = QLabel(self)
       self.label1.setFixedSize(700, 700)
       self.label1.move(50, 50)
       self.label1.setStyleSheet("QLabel{background:white;}")
       # set icon of your Window
       window_icon = QtGui.QIcon('./icon/icon.png') 
       self.setWindowIcon(window_icon)
       self.show()

   def show_img(self):

   	   img_pil = Image.fromarray(self.init_img)
   	   self.__reshape__(img_pil,self.init_img.shape)

   def __reshape__(self,img_pil,shape):
       if np.argmax(shape[:2]) == 1:
           image = img_pil.toqpixmap().scaled(self.label1.width(), self.label1.width()*shape[0]/shape[1])
       elif np.argmax(shape[:2]) == 0:
           image = img_pil.toqpixmap().scaled(self.label1.width()*shape[1]/shape[0],self.label1.width())
       
       self.label1.setPixmap(image)

   def closeEvent(self, event):
       
       reply = QMessageBox.question(self, 'Close',
           "Quit?", QMessageBox.Yes |  QMessageBox.No, QMessageBox.No)

       if reply == QMessageBox.Yes:
           event.accept()
       else:
           event.ignore()  
if __name__ == "__main__":
   app = QtWidgets.QApplication(sys.argv)
   test = MazeWindow()
   sys.exit(app.exec_())

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值