Ramón Wilhelm
Ramón Wilhelm

Reputation: 997

Python PyQt5: How to get the name and the content of an image file by QFileDialog

How to open an image file with QFileDialog? I also want to know how to get the name of the file e.g. the image file is named "photo.png" and I want to print its name.

def openImage(self):
    fileName = QFileDialog(self)
    fileName.getOpenFileName()
    image = QImage(fileName)
    print(fileName.FileName)

Upvotes: 0

Views: 1790

Answers (1)

Ramón Wilhelm
Ramón Wilhelm

Reputation: 997

I resolve the problem on my own:

def openImage(self):
    fileName = QFileDialog().getOpenFileName()
    filePath = str(fileName[0])

    image = QImage(filePath)

    label = QLabel()
    label.setPixmap(QPixmap().fromImage(image))

    self.addWidget(label, 0, 0)

    fileObject = filePath.split('/')
    file = fileObject[ len(fileObject) - 1 ]
    print(file)

Upvotes: 1

Related Questions