-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw.py
169 lines (130 loc) · 5.01 KB
/
draw.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# importing libraries
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
# window class
class Window(QMainWindow):
def __init__(self):
super().__init__()
# self.setCentralWidget(self.maintabs)
# setting title
self.setWindowTitle("Paint with PyQt5")
# setting geometry to main window
self.setGeometry(28*10, 28*10, 28*10, 28*10)
# creating image object
self.image = QImage(self.size(), QImage.Format_RGB32)
# making image color to white
self.image.fill(Qt.white)
# variables
# drawing flag
self.drawing = False
# default brush size
self.brushSize = 24
# default color
self.brushColor = Qt.black
# QPoint object to tract the point
self.lastPoint = QPoint()
# creating menu bar
mainMenu = self.menuBar()
# creating file menu for save and clear action
fileMenu = mainMenu.addMenu("File")
# adding brush size to main menu
# adding brush color to ain menu
b_color = mainMenu.addMenu("Brush Color")
# creating save action
saveAction = QAction("Save", self)
# adding short cut for save action
saveAction.setShortcut("Ctrl + S")
# adding save to the file menu
fileMenu.addAction(saveAction)
# adding action to the save
saveAction.triggered.connect(self.save)
# creating clear action
clearAction = QAction("Clear", self)
# adding short cut to the clear action
clearAction.setShortcut("Ctrl + C")
# adding clear to the file menu
fileMenu.addAction(clearAction)
# adding action to the clear
clearAction.triggered.connect(self.clear)
pix_12 = QAction("24px", self)
pix_12.triggered.connect(self.Pixel_12)
# creating options for brush color
# creating action for black color
black = QAction("Black", self)
# adding this action to the brush colors
b_color.addAction(black)
# adding methods to the black
black.triggered.connect(self.blackColor)
# similarly repeating above steps for different color
white = QAction("White", self)
b_color.addAction(white)
white.triggered.connect(self.whiteColor)
# method for checking mouse cicks
def mousePressEvent(self, event):
# if left mouse button is pressed
if event.button() == Qt.LeftButton:
# make drawing flag true
self.drawing = True
# make last point to the point of cursor
self.lastPoint = event.pos()
# method for tracking mouse activity
def mouseMoveEvent(self, event):
# checking if left button is pressed and drawing flag is true
if (event.buttons() & Qt.LeftButton) & self.drawing:
# creating painter object
painter = QPainter(self.image)
# set the pen of the painter
painter.setPen(QPen(self.brushColor, self.brushSize,
Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
# draw line from the last point of cursor to the current point
# this will draw only one step
painter.drawLine(self.lastPoint, event.pos())
# change the last point
self.lastPoint = event.pos()
# update
self.update()
# method for mouse left button release
def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton:
# make drawing flag false
self.drawing = False
# paint event
def paintEvent(self, event):
# create a canvas
canvasPainter = QPainter(self)
# draw rectangle on the canvas
canvasPainter.drawImage(self.rect(), self.image, self.image.rect())
# method for saving canvas
def save(self):
filePath, _ = QFileDialog.getSaveFileName(self, "Save Image", "",
"PNG(*.png);;JPEG(*.jpg *.jpeg);;All Files(*.*) ")
if filePath == "":
return
self.image.save(filePath)
f = open('path.txt', 'w')
f.write(filePath)
# method for clearing every thing on canvas
def clear(self):
# make the whole canvas white
self.image.fill(Qt.white)
# update
self.update()
# methods for changing pixel sizes
def Pixel_12(self):
self.brushSize = 12
# methods for changing brush color
def blackColor(self):
self.brushColor = Qt.black
def whiteColor(self):
self.brushColor = Qt.white
if __name__ == '__main__':
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# showing the wwindow
window.show()
# start the app
sys.exit(App.exec())