Saturday, November 16, 2024
Google search engine
HomeLanguagesPython – How to save canvas in pyqt5?

Python – How to save canvas in pyqt5?

There are so many options provided by Python to develop GUI application and PyQt5 is one of them. PyQt5 is cross-platform GUI toolkit, a set of python bindings for Qt v5. One can develop an interactive desktop application with so much ease because of the tools and simplicity provided by this library.

In this tutorial we’ll take a look how to save canvas in pyqt5. We draw things on canvas using QPainter class.

QPainter : Qt’s painting system is able to render vector graphics, images, and outline font-based text with sub-pixel accuracy using anti-aliasing to improve rendering quality.

In this article, we create a canvas and will draw a line on it and see how to save that canvas as image file at desired location on the system. Below is the implementation. 

Python3




# importing modules
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import * import sys
 
# creating class for window
class Window(QMainWindow):
    def __init__(self):
        super().__init__()
 
 
        title = "Paint and save Application"
 
        top = 400
        left = 400
        width = 800
        height = 600
 
        # setting title of window
        self.setWindowTitle(title)
 
        # setting geometry
        self.setGeometry(top, left, width, height)
 
        # creating canvas
        self.image = QImage(self.size(), QImage.Format_RGB32)
 
        # setting canvas color to white
        self.image.fill(Qt.white)
 
        # creating menu bar
        mainMenu = self.menuBar()
 
        # adding file menu in it
        fileMenu = mainMenu.addMenu("File")
 
        # creating save action
        saveAction = QAction("Save", self)
 
        # setting save action shortcut
        saveAction.setShortcut("Ctrl + S")
 
        # adding save action to filemenu
        fileMenu.addAction(saveAction)
 
        # setting triggered method
        saveAction.triggered.connect(self.save)
 
        # calling draw_something method
        self.draw_something()
 
    # paintEvent for creating blank canvas
    def paintEvent(self, event):
        canvasPainter = QPainter(self)
        canvasPainter.drawImage(self.rect(), self.image,
                                      self.image.rect())
         
    # this method will draw a line
    def draw_something(self):
         
        painter = QPainter(self.image)
         
        painter.setPen(QPen(Qt.black, 5, Qt.SolidLine,
                            Qt.RoundCap, Qt.RoundJoin))
         
        # drawing a line
        painter.drawLine(100, 100, 300, 300)
         
        # updating it to canvas
        self.update()
     
    # save method
    def save(self):
         
        # selecting file path
        filePath, _ = QFileDialog.getSaveFileName(self, "Save Image", "",
                         "PNG(*.png);;JPEG(*.jpg *.jpeg);;All Files(*.*) ")
 
        # if file path is blank return back
        if filePath == "":
            return
         
        # saving canvas at desired path
        self.image.save(filePath)
 
# main method
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Window()
    window.show()
     
    # looping for window
    sys.exit(app.exec())


Output : 
After running the code window will appear with a canvas of white color on which line is drawn. 

After clicking on the file menu save button will appear. 
 

When save button is pressed it will ask for the location and name and image will get saved at desirable location. 

This code will save the canvas in png format at the desired location.
 

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments