Last Updated on 2021-05-18 by Clay
When we use PyQt5 to develop a graphical interface, maybe sometimes we need to save the image of QLabel component. Of course, if we proactively show a picture on QLabel, we don't need to worry about this problem. OF COURSE WE ALREADY HAVE THAT DREAM PICTURE.
Today I encountered such a demand, and did not find a more appropriate solution on the Internet. After groping, the problem was finally solved, and it was also recorded in following.
Save image of QLabel
Use PIL package to save smoothly.
# -*- coding: utf-8 -*- import sys from PyQt5.QtGui import * from PyQt5.QtWidgets import * from PIL import ImageQt class MainWindow(QWidget): def __init__(self): super(MainWindow, self).__init__() self.resize(800, 600) self.setWindowTitle('Painter Board') self.label = QLabel(self) self.label.setGeometry(0, 0, 800, 600) self.label.setPixmap(QPixmap("../../Pictures/Saved Pictures/dog.jpg")) self.label.setScaledContents(True) self.add = QShortcut(QKeySequence("Ctrl+S"), self) self.add.activated.connect(self.compositeEvent) def compositeEvent(self): image = ImageQt.fromqpixmap(self.label.pixmap()) image.save('test.png') if __name__ == '__main__': app = QApplication([]) window = MainWindow() window.show() sys.exit(app.exec_())
Output:
The above is a simple function of using QLabel to display the picture we want. If you are interested in PyQt5, you can refer to I wrote before: [PyQt5] Tutorial(3) QMainWindow, QIcon, QPixmap, QPalette.
And then we press Ctrl + s
, according to the function set by my shortcut key, I will use ImageQt in PIL to read the pixmap of the Label and save it as test.png.
We try to open the save file:
It seems normal. We can save the pictures in this way.
Save text of QLabel as picture
The next part is the more difficult part to find the answer. First, we cancel the QLabel that just used QPixmap to display pictures and change it to display text.
# -*- coding: utf-8 -*- import sys from PyQt5.QtGui import * from PyQt5.QtWidgets import * from PIL import ImageQt class MainWindow(QWidget): def __init__(self): super(MainWindow, self).__init__() self.resize(800, 600) self.setWindowTitle('Painter Board') self.label = QLabel(self) self.label.setGeometry(0, 0, 800, 600) self.label.setText('Good day!') self.add = QShortcut(QKeySequence("Ctrl+S"), self) self.add.activated.connect(self.compositeEvent) def compositeEvent(self): image = ImageQt.fromqpixmap(self.label.pixmap()) image.save('test.png') if __name__ == '__main__': app = QApplication([]) window = MainWindow() window.show() sys.exit(app.exec_())
Output:
Just like this. And now we press ctrl + s
to save file again. But we will get an error message:
AttributeError: 'NoneType' object has no attribute 'hasAlphaChannel'
Obviously, QLabel displaying text is not suitable for such storage at all.
So, we need to change the following code:
image = ImageQt.fromqpixmap(self.label.pixmap())
Change to:
image = ImageQt.fromqpixmap(self.label.grab())
And then we can save it as picture.