pyqt5显示文件名和图片

代码如下

import sys
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class picture(QWidget):
    def __init__(self):
        super(picture, self).__init__()

        self.resize(600, 400)
        self.setWindowTitle("label显示图片")

        self.label = QLabel(self)
        self.label.setText("   显示图片")
        self.label.setFixedSize(int(410*2/3),int(410*2/3))
        self.label.move(160, 100)

        self.label.setStyleSheet("QLabel{background:white;}" "QLabel{color:rgb(300,300,300,120);font-size:15px;font-weight:bold;font-family:宋体;}")

        btn = QPushButton(self)
        btn.setText("打开图片")
        btn.move(10, 30)
        btn.clicked.connect(self.openimage)
        
        cbtn = QPushButton(self)
        cbtn.setText("选择文件")
        cbtn.move(10, 60)
        cbtn.clicked.connect(self.changefiles)
        
        self.labels = QLabel(self)
        self.labels.setText('file                                                                                  ')
        self.labels.move(150, 60)
        
    def openimage(self):
        imgName, imgType = QFileDialog.getOpenFileName(self, "打开图片", "", "*.jpg;;*.png;;All Files(*)")
        print(imgName)
        jpg = QtGui.QPixmap(imgName).scaled(self.label.width(), self.label.height())
        self.label.setPixmap(jpg)
    
    def changefiles(self):
        # directory1 = QFileDialog.getExistingDirectory(self,"选取文件夹","./")   #起始路径
        # print(directory1)

        filePath, filetype = QFileDialog.getOpenFileName(self, "选取文件", "./","All Files(*)")
        print(filePath)
        self.labels.setText(filePath)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    my = picture()
    my.show()
    sys.exit(app.exec_())

运行结果:
在这里插入图片描述
打开图片:
在这里插入图片描述
选择文件:
在这里插入图片描述

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个简单的Python PyQt5实现小波变换图像增强并将图像显示在QLabel的代码示例,注释比较详细: ```python import pywt import numpy as np from PyQt5.QtGui import QImage, QPixmap from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QFileDialog class MainWindow(QMainWindow): def __init__(self): super().__init__() # 创建QLabel用于显示图像 self.label = QLabel(self) self.setCentralWidget(self.label) # 打开图像文件 self.filename, _ = QFileDialog.getOpenFileName(self, 'Open file', '', 'Image files (*.jpg *.png *.bmp)') # 载入图像数据并将其转换为灰度图像 img = QImage(self.filename) img = img.convertToFormat(QImage.Format_Grayscale8) self.image = np.array(img.constBits()).reshape(img.height(), img.width()) # 创建小波变换对象 self.wavelet = pywt.Wavelet('db1') # 进行一级小波分解 coeffs = pywt.wavedec2(self.image, self.wavelet, level=1) # 对低频分量进行直方图均衡化 coeffs[0] = pywt.threshold(coeffs[0], np.std(coeffs[0]) / 2, mode='soft') coeffs[0] = pywt.threshold(coeffs[0], np.std(coeffs[0]) / 2, mode='hard') coeffs[0] = pywt.threshold(coeffs[0], np.std(coeffs[0]) / 2, mode='greater') coeffs[0] = pywt.threshold(coeffs[0], np.std(coeffs[0]) / 2, mode='less') coeffs[0] = pywt.threshold(coeffs[0], np.std(coeffs[0]) / 2, mode='garotte') # 进行一级小波重构 self.image = pywt.waverec2(coeffs, self.wavelet) # 创建QPixmap对象并将图像数据转换为QImage对象 qimg = QImage(self.image, self.image.shape[1], self.image.shape[0], self.image.shape[1], QImage.Format_Grayscale8) qpixmap = QPixmap(qimg) # 将图像显示在QLabel self.label.setPixmap(qpixmap) self.label.setFixedSize(qpixmap.width(), qpixmap.height()) if __name__ == '__main__': app = QApplication([]) window = MainWindow() window.show() app.exec_() ``` 这段代码,我们首先创建了一个QLabel用于显示图像。然后,我们通过QFileDialog打开图像文件,并将其转换为灰度图像。接着,我们使用pywt.Wavelet创建了一个小波变换对象,通过pywt.wavedec2进行一级小波分解,对低频分量进行直方图均衡化,再通过pywt.waverec2进行一级小波重构。最后,我们将图像数据转换为QImage对象,并通过QPixmap将其显示在QLabel
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

☆程序小黑★

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值