From f6e615602a36a9fd91dbf53dc880588bad830c4d Mon Sep 17 00:00:00 2001 From: Prajjwal Datir <46681482+PrajjwalDatir@users.noreply.github.com> Date: Sun, 25 Oct 2020 16:39:06 +0530 Subject: [PATCH 1/2] Added Python Paint Application this is a pretty basic Python PyQT based Paint Application. Code is well commented for understanding. --- paint.py | 195 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 195 insertions(+) create mode 100644 paint.py diff --git a/paint.py b/paint.py new file mode 100644 index 0000000..17e57a1 --- /dev/null +++ b/paint.py @@ -0,0 +1,195 @@ +from PyQt5.QtWidgets import QMainWindow, QApplication, QMenu, QMenuBar, QAction, QFileDialog +from PyQt5.QtGui import QIcon, QImage, QPainter, QPen, QBrush +from PyQt5.QtCore import Qt, QPoint +import sys + +class Window(QMainWindow): + def __init__(self): + super().__init__() + + + title = "Prajjwal's Paint Application" + x = 500 + y = 100 + width = 1080 + height = 720 + + icon = "icons/Code_Maniac.jpg" + + self.setWindowTitle(title) + self.setGeometry(x, y, width, height) + self.setWindowIcon(QIcon(icon)) + + self.image = QImage(self.size(), QImage.Format_RGB32) + self.image.fill(Qt.white) + + self.drawing = False + self.brushSize = 2 + self.brushColor = Qt.black + self.lastPoint = QPoint() + + mainMenu = self.menuBar() + fileMenu = mainMenu.addMenu("File") + brushSize = mainMenu.addMenu("Brush Size") + brushColor = mainMenu.addMenu("Brush Color") + shapes = mainMenu.addMenu("Add Shapes") + + circleAction = QAction(QIcon("icons/save.png"), "Cicle", self) + circleAction.setShortcut("Alt+C") + shapes.addAction(circleAction) + circleAction.triggered.connect(self.circle) + + rectAction = QAction(QIcon("icons/save.png"), "Rectangle", self) + rectAction.setShortcut("Alt+R") + shapes.addAction(rectAction) + rectAction.triggered.connect(self.rectangle) + + + saveAction = QAction(QIcon("icons/save.png"), "Save",self) + saveAction.setShortcut("Ctrl+S") + fileMenu.addAction(saveAction) + saveAction.triggered.connect(self.save) + + clearAction = QAction(QIcon("icons/clear.png"), "Clear", self) + clearAction.setShortcut("Ctrl+C") + fileMenu.addAction(clearAction) + clearAction.triggered.connect(self.clear) + + threepxAction = QAction("3px", self) + brushSize.addAction(threepxAction) + threepxAction.triggered.connect(self.threePixel) + + fivepxAction = QAction("5px", self) + brushSize.addAction(fivepxAction) + fivepxAction.triggered.connect(self.fivePixel) + + sevenpxAction = QAction("7px", self) + brushSize.addAction(sevenpxAction) + sevenpxAction.triggered.connect(self.sevenPixel) + + ninepxAction = QAction("9px", self) + brushSize.addAction(ninepxAction) + ninepxAction.triggered.connect(self.ninePixel) + + blackAction = QAction(QIcon("icons/black.png"), "Black", self) + blackAction.setShortcut("Ctrl+B") + brushColor.addAction(blackAction) + blackAction.triggered.connect(self.blackColor) + + + whitekAction = QAction(QIcon("icons/white.png"), "White", self) + whitekAction.setShortcut("Ctrl+W") + brushColor.addAction(whitekAction) + whitekAction.triggered.connect(self.whiteColor) + + + redAction = QAction(QIcon("icons/red.png"), "Red", self) + redAction.setShortcut("Ctrl+R") + brushColor.addAction(redAction) + redAction.triggered.connect(self.redColor) + + greenAction = QAction(QIcon("icons/green.png"), "Green", self) + greenAction.setShortcut("Ctrl+G") + brushColor.addAction(greenAction) + greenAction.triggered.connect(self.greenColor) + + yellowAction = QAction(QIcon("icons/yellow.png"), "Yellow", self) + yellowAction.setShortcut("Ctrl+Y") + brushColor.addAction(yellowAction) + yellowAction.triggered.connect(self.yellowColor) + + def mousePressEvent(self, event): + if event.button() == Qt.LeftButton: + self.drawing = True + self.lastPoint = event.pos() + #print(self.lastPoint) + + + def exit_paint(self, event): + if event.type() == KEY_DOWN: + if event.key() == Qt.Key_Escape: + self.close() + + def mouseMoveEvent(self, event): + if(event.buttons() & Qt.LeftButton) & self.drawing: + painter = QPainter(self.image) + painter.setPen(QPen(self.brushColor, self.brushSize, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin)) + painter.drawLine(self.lastPoint, event.pos()) + self.lastPoint = event.pos() + self.update() + + + + def mouseReleaseEvent(self, event): + + if event.button() == Qt.LeftButton: + self.drawing = False + + + def paintEvent(self, event): + canvasPainter = QPainter(self) + canvasPainter.drawImage(self.rect(),self.image, self.image.rect() ) + + + + + + def save(self): + filePath, _ = QFileDialog.getSaveFileName(self, "Save Image", "", "PNG(*.png);;JPEG(*.jpg *.jpeg);;All Files(*.*) ") + + if filePath == "": + return + self.image.save(filePath) + + + + def clear(self): + self.image.fill(Qt.white) + self.update() + + + def threePixel(self): + self.brushSize = 3 + + def fivePixel(self): + self.brushSize = 5 + + def sevenPixel(self): + self.brushSize = 7 + + def ninePixel(self): + self.brushSize = 9 + + + def blackColor(self): + self.brushColor = Qt.black + + def whiteColor(self): + self.brushColor = Qt.white + + def redColor(self): + self.brushColor = Qt.red + + def greenColor(self): + self.brushColor = Qt.green + + def yellowColor(self): + self.brushColor = Qt.yellow + + + def circle(self): + painter = QPainter(self) + painter.setPen(QPen(Qt.green, 5, Qt.SolidLine)) + painter.setBrush(QBrush(Qt.green, Qt.SolidPattern)) + painter.drawEllipse(40, 40, 400, 200) + pass + + + def rectangle(self): + pass + +if __name__ == "__main__": + app = QApplication(sys.argv) + window = Window() + window.show() + app.exec() \ No newline at end of file From 8f240dea406c7cd5190416df699bfc4a75be5991 Mon Sep 17 00:00:00 2001 From: Prajjwal Datir <46681482+PrajjwalDatir@users.noreply.github.com> Date: Sun, 25 Oct 2020 16:40:42 +0530 Subject: [PATCH 2/2] Added Paint App using Python and PyQT --- paint.py => paintApplicationPython.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename paint.py => paintApplicationPython.py (99%) diff --git a/paint.py b/paintApplicationPython.py similarity index 99% rename from paint.py rename to paintApplicationPython.py index 17e57a1..df98e22 100644 --- a/paint.py +++ b/paintApplicationPython.py @@ -192,4 +192,4 @@ def rectangle(self): app = QApplication(sys.argv) window = Window() window.show() - app.exec() \ No newline at end of file + app.exec()