-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreal_drawing_board.py
More file actions
356 lines (267 loc) · 11.3 KB
/
Copy pathreal_drawing_board.py
File metadata and controls
356 lines (267 loc) · 11.3 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from datetime import datetime
from classification import classification
from Fill_color import Fill_color
import copy
import cv2
from PyQt5.QtWidgets import QMessageBox
class drawing_board(QWidget):
def __init__(self):
super().__init__()
QApplication.setAttribute(Qt.AA_EnableHighDpiScaling, True) # 화면크기스케일링
self.file = ''
self.file2 = ''
# 전체 폼 박스
formbox = QHBoxLayout()
self.setLayout(formbox)
# 좌, 우 레이아웃박스
left = QVBoxLayout()
right = QVBoxLayout()
self.right2 = QVBoxLayout()
# 그룹박스1 생성 및 좌 레이아웃 배치
gb = QGroupBox('그리기 종류')
left.addWidget(gb)
# 그룹박스1 에서 사용할 레이아웃
box = QVBoxLayout()
gb.setLayout(box)
# 그룹박스 1 의 라디오 버튼 배치
text = ['line', 'Curve', 'Rectange', 'Ellipse']
self.radiobtns = []
for i in range(len(text)):
self.radiobtns.append(QRadioButton(text[i], self))
self.radiobtns[i].clicked.connect(self.radioClicked)
box.addWidget(self.radiobtns[i])
self.radiobtns[1].setChecked(True)
self.drawType = 1
# 그룹박스2
gb = QGroupBox('펜 설정')
left.addWidget(gb)
grid = QGridLayout()
gb.setLayout(grid)
label = QLabel('선굵기')
grid.addWidget(label, 0, 0)
self.combo = QComboBox()
grid.addWidget(self.combo, 0, 1)
for i in range(4, 21):
self.combo.addItem(str(i))
label = QLabel('선색상')
grid.addWidget(label, 1, 0)
self.pencolor = QColor(0, 0, 0)
self.penbtn = QPushButton()
self.penbtn.setStyleSheet('background-color: rgb(0,0,0)')
self.penbtn.clicked.connect(self.showColorDlg)
grid.addWidget(self.penbtn, 1, 1)
# 그룹박스3
gb = QGroupBox('붓 설정')
left.addWidget(gb)
hbox = QHBoxLayout()
gb.setLayout(hbox)
label = QLabel('붓색상')
hbox.addWidget(label)
self.brushcolor = QColor(255, 255, 255)
self.brushbtn = QPushButton()
self.brushbtn.setStyleSheet('background-color: rgb(255,255,255)')
self.brushbtn.clicked.connect(self.showColorDlg)
hbox.addWidget(self.brushbtn)
# 그룹박스4
gb = QGroupBox('지우개')
left.addWidget(gb)
hbox = QHBoxLayout()
gb.setLayout(hbox)
self.checkbox = QCheckBox('지우개 동작')
self.checkbox.stateChanged.connect(self.checkClicked)
hbox.addWidget(self.checkbox)
# 전체 지우기
removebutton = QPushButton('전체 지우기', self)
left.addWidget(removebutton)
removebutton.clicked.connect(self.remove_all)
# 사진저장 버튼
# savebutton = QPushButton('그림 저장', self)
# left.addWidget(savebutton)
# savebutton.clicked.connect(self.save_image)
# painting 버튼
paintingbutton = QPushButton('자동 채색', self)
left.addWidget(paintingbutton)
paintingbutton.clicked.connect(self.load_image)
left.addStretch(1) # 그냥 레이아웃 여백 추가
# 우 레이아웃 박스에 그래픽 뷰 추가
self.view = CView(self)
self.view.setFixedWidth(300)
self.view.setFixedHeight(300)
right.addWidget(self.view)
# 제일 오른쪽 레이아웃에 빈 흰색 배경
pixmap = QPixmap('whiteimage.png')
self.lbl_img = QLabel()
self.lbl_img.setPixmap(pixmap)
self.right2.addWidget(self.lbl_img)
# 전체 폼박스에 레이아웃 박스 배치
formbox.addLayout(left)
formbox.addLayout(right)
formbox.addLayout(self.right2)
formbox.setStretchFactor(left, 0)
formbox.setStretchFactor(right, 1)
self.setGeometry(100, 100, 700, 400)
def radioClicked(self):
for i in range(len(self.radiobtns)):
if self.radiobtns[i].isChecked():
self.drawType = i
break
def checkClicked(self):
pass
def showColorDlg(self):
# 색상 대화상자 생성
color = QColorDialog.getColor()
sender = self.sender()
# 색상이 유효한 값이면 참, QFrame에 색 적용
if sender == self.penbtn and color.isValid():
self.pencolor = color
self.penbtn.setStyleSheet('background-color: {}'.format(color.name()))
else:
self.brushcolor = color
self.brushbtn.setStyleSheet('background-color: {}'.format(color.name()))
def save_image(self):
date = datetime.now()
filename = 'Screenshot ' + date.strftime('%Y-%m-%d_%H-%M-%S.png')
img = QPixmap(self.view.grab(self.view.sceneRect().toRect()))
self.file = "./multi_img_data/imgs_others_test_sketch/" + filename
img.save(self.file, 'png')
img = cv2.imread(self.file, 0)
img = img[2:476, 2:663] #사진 저장할 때 끝부분 모서리 지우기
cv2.imwrite(self.file, img)
def remove_all(self):
for i in self.view.scene.items():
self.view.scene.removeItem(i)
def load_image(self):
self.save_image()
label = classification().label # 이미지 분류
# print(label)
reply = self.msg_box('\"'+ label +'\"을 그린 게 맞다면 \"OK\" 버튼을, 다시 그리고 싶다면 \"NO\" 버튼을 눌러주세요.')
if reply == True:
self.lbl_img.hide() # 전 이미지 숨김
if label == "apple":
self.file2 = copy.copy(self.file)
fill = Fill_color(self.file, label, 1) #이미지 색칠
self.file = fill.file
# print(self.file)
pixmap = QPixmap(self.file) # jpg 는 안되는데 왜 안되는 지 아직 모르겠다..
self.lbl_img = QLabel()
self.lbl_img.setPixmap(pixmap)
self.right2.addWidget(self.lbl_img)
fill = Fill_color(self.file2, label, 2) #이미지 색칠
self.file2 = fill.file
# print(self.file)
pixmap = QPixmap(self.file2) # jpg 는 안되는데 왜 안되는 지 아직 모르겠다..
self.lbl_img = QLabel()
self.lbl_img.setPixmap(pixmap)
self.right2.addWidget(self.lbl_img)
else:
fill = Fill_color(self.file, label, 1) #이미지 색칠
self.file = fill.file
# print(self.file)
pixmap = QPixmap(self.file) # jpg 는 안되는데 왜 안되는 지 아직 모르겠다..
self.lbl_img = QLabel()
self.lbl_img.setPixmap(pixmap)
self.right2.addWidget(self.lbl_img)
def msg_box(self, text):
msgBox = QMessageBox()
msgBox.setText(text)
reply = msgBox.question(self, 'message', text, QMessageBox.Ok | QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.No:
return False
else: return True
# QGraphicsView display QGraphicsScene
class CView(QGraphicsView):
def __init__(self, parent):
super().__init__(parent)
self.scene = QGraphicsScene()
self.setScene(self.scene)
self.items = []
self.start = QPointF()
self.end = QPointF()
self.setRenderHint(QPainter.HighQualityAntialiasing)
def moveEvent(self, e):
rect = QRectF(self.rect())
rect.adjust(0, 0, -4, -4)
self.scene.setSceneRect(rect)
def mousePressEvent(self, e):
if e.button() == Qt.LeftButton:
# 시작점 저장
self.start = e.pos()
self.end = e.pos()
def mouseMoveEvent(self, e):
# e.buttons()는 정수형 값을 리턴, e.button()은 move시 Qt.Nobutton 리턴
if e.buttons() & Qt.LeftButton:
self.end = e.pos()
if self.parent().checkbox.isChecked():
pen = QPen(QColor(255, 255, 255), 10)
path = QPainterPath()
path.moveTo(self.start)
path.lineTo(self.end)
self.scene.addPath(path, pen)
self.start = e.pos()
return None
pen = QPen(self.parent().pencolor, self.parent().combo.currentIndex()+3)
# 직선 그리기
if self.parent().drawType == 0:
# 장면에 그려진 이전 선을 제거
if len(self.items) > 0:
self.scene.removeItem(self.items[-1])
del (self.items[-1])
# 현재 선 추가
line = QLineF(self.start.x(), self.start.y(), self.end.x(), self.end.y())
self.items.append(self.scene.addLine(line, pen))
# 곡선 그리기
if self.parent().drawType == 1:
# Path 이용
path = QPainterPath()
path.moveTo(self.start)
path.lineTo(self.end)
self.scene.addPath(path, pen)
# Line 이용
# line = QLineF(self.start.x(), self.start.y(), self.end.x(), self.end.y())
# self.scene.addLine(line, pen)
# 시작점을 다시 기존 끝점으로
self.start = e.pos()
# 사각형 그리기
if self.parent().drawType == 2:
brush = QBrush(self.parent().brushcolor)
if len(self.items) > 0:
self.scene.removeItem(self.items[-1])
del (self.items[-1])
rect = QRectF(self.start, self.end)
self.items.append(self.scene.addRect(rect, pen, brush))
# 원 그리기
if self.parent().drawType == 3:
brush = QBrush(self.parent().brushcolor)
if len(self.items) > 0:
self.scene.removeItem(self.items[-1])
del (self.items[-1])
rect = QRectF(self.start, self.end)
self.items.append(self.scene.addEllipse(rect, pen, brush))
def mouseReleaseEvent(self, e):
if e.button() == Qt.LeftButton:
if self.parent().checkbox.isChecked():
return None
pen = QPen(self.parent().pencolor, self.parent().combo.currentIndex())
if self.parent().drawType == 0:
self.items.clear()
line = QLineF(self.start.x(), self.start.y(), self.end.x(), self.end.y())
self.scene.addLine(line, pen)
if self.parent().drawType == 2:
brush = QBrush(self.parent().brushcolor)
self.items.clear()
rect = QRectF(self.start, self.end)
self.scene.addRect(rect, pen, brush)
if self.parent().drawType == 3:
brush = QBrush(self.parent().brushcolor)
self.items.clear()
rect = QRectF(self.start, self.end)
self.scene.addEllipse(rect, pen, brush)
# if __name__ == '__main__':
# app = QApplication(sys.argv)
# w = drawing_board()
# w.show()
# sys.exit(app.exec_())