Open In App
Related Articles

Python – How to save canvas in pyqt5?

Improve Article
Save Article
Like Article

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. 


# 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):
        title = "Paint and save Application"
        top = 400
        left = 400
        width = 800
        height = 600
        # setting title of window
        # setting geometry
        self.setGeometry(top, left, width, height)
        # creating canvas
        self.image = QImage(self.size(), QImage.Format_RGB32)
        # setting canvas color to 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
        # setting triggered method
        # calling draw_something method
    # paintEvent for creating blank canvas
    def paintEvent(self, event):
        canvasPainter = QPainter(self)
        canvasPainter.drawImage(self.rect(), self.image,
    # this method will draw a line
    def draw_something(self):
        painter = QPainter(self.image)
        painter.setPen(QPen(, 5, Qt.SolidLine,
                            Qt.RoundCap, Qt.RoundJoin))
        # drawing a line
        painter.drawLine(100, 100, 300, 300)
        # updating it to canvas
    # 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 == "":
        # saving canvas at desired path
# main method
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Window()
    # looping for window

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.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 25 Nov, 2021
Like Article
Save Article
Similar Reads
Complete Tutorials