回答編集履歴
2
teamiklの指摘あり修正
test
CHANGED
@@ -1,18 +1,42 @@
|
|
1
|
+
※投稿後追記:
|
2
|
+
|
1
|
-
|
3
|
+
動くコードにはなっていますが、当方の知識不足の点が多々あると思います。上のteamiklさんの回答の方が正確なため、teamiklさんの回答&コードを参考にしてください。
|
2
|
-
|
3
|
-
|
4
|
-
|
4
|
+
|
5
|
+
|
6
|
+
|
5
|
-
|
7
|
+
① 削除
|
6
|
-
|
8
|
+
|
7
|
-
|
9
|
+
② PredictCanvasで下記のようにpaintEventを実装してあげないと、描画更新されません。
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
10
|
+
|
12
|
-
|
11
|
+
|
12
|
+
|
13
|
-
```
|
13
|
+
```
|
14
|
+
|
14
|
-
|
15
|
+
def paintEvent(self, event):
|
16
|
+
|
17
|
+
painter = QPainter(self)
|
18
|
+
|
15
|
-
|
19
|
+
rect = event.rect()
|
20
|
+
|
21
|
+
painter.drawImage(rect, self.image_predict, rect)
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
```
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
③ 元コードでは、ToolFrame(QWidget)で新たにPredictCanvasが生成されてしまっており、メインウィジェットのpredict(predict_canvas)が共有されていません。
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
下記のように、メインウィジェットのpredict_canvasをToolFrame(QWidget)が受け取れるように修正する必要があります。
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
呼び出し元
|
38
|
+
|
39
|
+
```
|
16
40
|
|
17
41
|
def initUI(self):
|
18
42
|
|
@@ -24,29 +48,423 @@
|
|
24
48
|
|
25
49
|
self.predict_canvas = PredictCanvas(frame)
|
26
50
|
|
51
|
+
tools = ToolFrame(frame, canvas=self.predict_canvas) #修正部分 メインウィジェットのpredict_canvsを引数として呼び出す。
|
52
|
+
|
53
|
+
```
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
呼び出し先
|
58
|
+
|
59
|
+
```
|
60
|
+
|
61
|
+
class ToolFrame(QWidget):
|
62
|
+
|
63
|
+
def __init__(self, parent=None, canvas = None): 修正
|
64
|
+
|
65
|
+
super().__init__(parent)
|
66
|
+
|
67
|
+
self.predict = canvas # 修正 メインウィジェットのpredict_canvasを受け取る
|
68
|
+
|
69
|
+
self.initUI()
|
70
|
+
|
71
|
+
```
|
72
|
+
|
73
|
+
--------------------------
|
74
|
+
|
75
|
+
以上をまとめた全体コードが、下記のコードです。
|
76
|
+
|
77
|
+
```
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
import sys
|
82
|
+
|
83
|
+
from PyQt5.QtWidgets import (
|
84
|
+
|
85
|
+
QWidget, QApplication, QMainWindow, QAction,
|
86
|
+
|
87
|
+
QFileDialog, QColorDialog, QInputDialog, QPushButton,QHBoxLayout, QVBoxLayout, QApplication)
|
88
|
+
|
89
|
+
from PyQt5.QtGui import QPainter, QImage, QPen, qRgb
|
90
|
+
|
91
|
+
from PyQt5.QtCore import Qt, QPoint, QRect, QSize, QDir
|
92
|
+
|
93
|
+
from collections import deque
|
94
|
+
|
95
|
+
from PyQt5.QtCore import pyqtSlot
|
96
|
+
|
97
|
+
from PyQt5.QtGui import QIcon
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
class MainWindow(QMainWindow):
|
102
|
+
|
103
|
+
def __init__(self):
|
104
|
+
|
105
|
+
super(MainWindow, self).__init__()
|
106
|
+
|
107
|
+
self.setupUI()
|
108
|
+
|
109
|
+
self.setCentralWidget(self.initUI())
|
110
|
+
|
111
|
+
#クラスを呼び出す。
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
def initUI(self):
|
116
|
+
|
117
|
+
frame = QWidget(self)
|
118
|
+
|
119
|
+
self.canvas = Canvas(frame)
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
self.predict_canvas = PredictCanvas(frame)
|
124
|
+
|
125
|
+
tools = ToolFrame(frame, canvas=self.predict_canvas)
|
126
|
+
|
27
127
|
hbox = QHBoxLayout(frame)
|
28
128
|
|
129
|
+
#vbox=QVBoxLayout()
|
130
|
+
|
131
|
+
#vbox.addStretch(0)
|
132
|
+
|
133
|
+
#vbox.addWidget(tools)
|
134
|
+
|
135
|
+
#vbox.addWidget(predict)
|
136
|
+
|
29
137
|
hbox.addWidget(self.canvas)
|
30
138
|
|
31
139
|
hbox.addWidget(self.predict_canvas)
|
32
140
|
|
33
141
|
hbox.addWidget(tools)
|
34
142
|
|
143
|
+
|
144
|
+
|
35
|
-
frame.setLayout(hbox)
|
145
|
+
# frame.setLayout(hbox) # 9行目前でQHBoxLayout(frame)というように親を指定しているため不要
|
146
|
+
|
147
|
+
|
36
148
|
|
37
149
|
return frame
|
38
150
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
151
|
+
|
152
|
+
|
153
|
+
def setupUI(self):
|
154
|
+
|
155
|
+
menubar = self.menuBar()
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
openAct = QAction('&Open', self)
|
160
|
+
|
161
|
+
openAct.setShortcut('Ctrl+O')
|
162
|
+
|
163
|
+
openAct.triggered.connect(self.openFile)
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
exitAct = QAction('&Exit', self)
|
168
|
+
|
169
|
+
exitAct.setShortcut('Ctrl+Q')
|
170
|
+
|
171
|
+
exitAct.triggered.connect(self.close)
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
saveAct = QAction('&Save', self)
|
176
|
+
|
177
|
+
saveAct.setShortcut('Ctrl+S')
|
178
|
+
|
179
|
+
saveAct.triggered.connect(self.saveFile)
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
fileMenu = menubar.addMenu('&File')
|
184
|
+
|
185
|
+
#fileMenu.addAction(resetAct)
|
186
|
+
|
187
|
+
fileMenu.addAction(openAct)
|
188
|
+
|
189
|
+
fileMenu.addAction(saveAct)
|
190
|
+
|
191
|
+
fileMenu.addAction(exitAct)
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
# 画像付き Pencolor selectするアクションオブジェクト作成
|
196
|
+
|
197
|
+
selectColorAction = QAction( QIcon('sample/white-bear.png'),'Pen Color', self)
|
198
|
+
|
199
|
+
selectColorAction.setShortcut('Ctrl+A')
|
200
|
+
|
201
|
+
selectColorAction.triggered.connect(self.selectColor)
|
202
|
+
|
203
|
+
self.toolbar = self.addToolBar('Pen Color')
|
204
|
+
|
205
|
+
self.toolbar.addAction(selectColorAction)
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
# ツールバー作成
|
210
|
+
|
211
|
+
selectwidthAction = QAction( QIcon('sample/white-bear.png'),'Selecwidth', self)
|
212
|
+
|
213
|
+
selectwidthAction.triggered.connect(self.selectWidth)
|
214
|
+
|
215
|
+
self.toolbar = self.addToolBar('Width')
|
216
|
+
|
217
|
+
self.toolbar.addAction(selectwidthAction)
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
# ツールバー作成
|
222
|
+
|
223
|
+
backAction = QAction( QIcon('sample/white-bear.png'),'Back', self)
|
224
|
+
|
225
|
+
backAction.setShortcut('Ctrl+Z')
|
226
|
+
|
227
|
+
backAction.triggered.connect(self.on_back)
|
228
|
+
|
229
|
+
self.toolbar = self.addToolBar('Undo')
|
230
|
+
|
231
|
+
self.toolbar.addAction(backAction)
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
# ツールバー作成
|
236
|
+
|
237
|
+
clearAction = QAction( QIcon('sample/white-bear.png'),'Clear', self)
|
238
|
+
|
239
|
+
clearAction.setShortcut('Ctrl+B')
|
240
|
+
|
241
|
+
clearAction.triggered.connect(self.on_reset)
|
242
|
+
|
243
|
+
self.toolbar = self.addToolBar('Undo')
|
244
|
+
|
245
|
+
self.toolbar.addAction(clearAction)
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
@pyqtSlot()
|
250
|
+
|
251
|
+
def on_back(self):
|
252
|
+
|
253
|
+
print('PyQt5 button click')
|
254
|
+
|
255
|
+
self.canvas.backImage()
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
@pyqtSlot()
|
260
|
+
|
261
|
+
def on_reset(self):
|
262
|
+
|
263
|
+
self.canvas.resetImage()
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
@pyqtSlot()
|
268
|
+
|
269
|
+
def on_run(self):
|
270
|
+
|
271
|
+
fileName, _ = QFileDialog.getOpenFileName(self, "Open File", QDir.currentPath())
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
if fileName:
|
276
|
+
|
277
|
+
self.predict_canvas.PredictImage(fileName)
|
278
|
+
|
279
|
+
#self.canvas.openImage(fileName)
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
def selectColor(self):
|
284
|
+
|
285
|
+
newColor = QColorDialog.getColor(self.canvas.penColor())
|
286
|
+
|
287
|
+
self.canvas.setPenColor(newColor)
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
def selectWidth(self):
|
292
|
+
|
293
|
+
newWidth, ok = QInputDialog.getInt(
|
294
|
+
|
295
|
+
self, "select",
|
296
|
+
|
297
|
+
"select pen width: ", self.canvas.penWidth(), 1, 100, 1)
|
298
|
+
|
299
|
+
if ok:
|
300
|
+
|
301
|
+
self.canvas.setPenWidth(newWidth)
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
def openFile(self):
|
306
|
+
|
307
|
+
fileName, _ = QFileDialog.getOpenFileName(self, "Open File", QDir.currentPath())
|
308
|
+
|
309
|
+
if fileName:
|
310
|
+
|
311
|
+
self.predict_canvas.PredictImage(fileName)
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
def saveFile(self):
|
316
|
+
|
317
|
+
path = QDir.currentPath()
|
318
|
+
|
319
|
+
print(path)
|
320
|
+
|
321
|
+
fileName, _ = QFileDialog.getSaveFileName(self, "Save as",path)
|
322
|
+
|
323
|
+
fileName=fileName+'.png'
|
324
|
+
|
325
|
+
if fileName:
|
326
|
+
|
327
|
+
print(fileName)
|
328
|
+
|
329
|
+
return self.canvas.saveImage(fileName)
|
330
|
+
|
331
|
+
else:
|
332
|
+
|
333
|
+
print("you couldnt save the file")
|
334
|
+
|
335
|
+
return False
|
336
|
+
|
337
|
+
|
338
|
+
|
339
|
+
class ToolFrame(QWidget):
|
340
|
+
|
341
|
+
def __init__(self, parent=None, canvas = None):
|
342
|
+
|
343
|
+
super().__init__(parent)
|
344
|
+
|
345
|
+
self.predict = canvas # PredictCanvas()
|
346
|
+
|
347
|
+
self.initUI()
|
348
|
+
|
349
|
+
|
350
|
+
|
351
|
+
def initUI(self):
|
352
|
+
|
353
|
+
button_predict = QPushButton('predict', self)
|
354
|
+
|
355
|
+
button_predict.clicked.connect(self.on_run)
|
356
|
+
|
357
|
+
|
358
|
+
|
359
|
+
|
360
|
+
|
361
|
+
button_reset = QPushButton('Reset', self)
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
vbox = QVBoxLayout(self)
|
366
|
+
|
367
|
+
vbox.addStretch(1)
|
368
|
+
|
369
|
+
vbox.addWidget(button_predict)
|
370
|
+
|
371
|
+
vbox.addWidget(button_reset)
|
372
|
+
|
373
|
+
|
374
|
+
|
375
|
+
@pyqtSlot()
|
376
|
+
|
377
|
+
def on_run(self):
|
378
|
+
|
379
|
+
fileName, _ = QFileDialog.getOpenFileName(self, "Open File", QDir.currentPath())
|
380
|
+
|
381
|
+
|
382
|
+
|
383
|
+
if fileName:
|
384
|
+
|
385
|
+
self.predict.PredictImage(fileName)
|
386
|
+
|
387
|
+
#self.canvas.openImage(fileName)
|
388
|
+
|
389
|
+
|
390
|
+
|
391
|
+
class Canvas(QWidget):
|
392
|
+
|
393
|
+
def __init__(self, parent = None):
|
394
|
+
|
395
|
+
super(Canvas, self).__init__(parent)
|
396
|
+
|
397
|
+
|
398
|
+
|
399
|
+
self.myPenWidth = 2
|
400
|
+
|
401
|
+
self.myPenColor = Qt.black
|
402
|
+
|
403
|
+
self.image = QImage()
|
404
|
+
|
405
|
+
self.check = False
|
406
|
+
|
407
|
+
self.back = deque(maxlen = 10)
|
408
|
+
|
409
|
+
self.image = QImage(300, 300, QImage.Format_RGB32)
|
410
|
+
|
411
|
+
self.image.fill(qRgb(255, 255, 255))
|
412
|
+
|
413
|
+
#self.next = deque(maxlen = 10)
|
414
|
+
|
415
|
+
# initUIはもう必要ないから消しておこうね
|
416
|
+
|
417
|
+
|
418
|
+
|
419
|
+
|
420
|
+
|
421
|
+
def mousePressEvent(self, event):
|
422
|
+
|
423
|
+
if event.button() == Qt.LeftButton:
|
424
|
+
|
425
|
+
self.back.append(self.resizeImage(self.image, self.image.size()))
|
426
|
+
|
427
|
+
self.lastPos = event.pos()
|
428
|
+
|
429
|
+
self.check = True
|
430
|
+
|
431
|
+
|
432
|
+
|
433
|
+
def mouseMoveEvent(self, event):
|
434
|
+
|
435
|
+
if event.buttons() and Qt.LeftButton and self.check:
|
436
|
+
|
437
|
+
self.drawLine(event.pos())
|
438
|
+
|
439
|
+
|
440
|
+
|
441
|
+
def mouseReleaseEvent(self, event):
|
442
|
+
|
443
|
+
if event.button() == Qt.LeftButton and self.check:
|
444
|
+
|
445
|
+
self.drawLine(event.pos())
|
446
|
+
|
447
|
+
self.check = False
|
448
|
+
|
449
|
+
|
450
|
+
|
451
|
+
def drawLine(self, endPos):
|
452
|
+
|
453
|
+
painter = QPainter(self.image)
|
454
|
+
|
455
|
+
painter.setPen(
|
456
|
+
|
457
|
+
QPen(self.myPenColor, self.myPenWidth, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin)
|
458
|
+
|
459
|
+
)
|
460
|
+
|
461
|
+
painter.drawLine(self.lastPos, endPos)
|
462
|
+
|
463
|
+
self.update()
|
464
|
+
|
465
|
+
self.lastPos = QPoint(endPos)
|
466
|
+
|
467
|
+
|
50
468
|
|
51
469
|
def paintEvent(self, event):
|
52
470
|
|
@@ -54,610 +472,156 @@
|
|
54
472
|
|
55
473
|
rect = event.rect()
|
56
474
|
|
475
|
+
painter.drawImage(rect, self.image, rect)
|
476
|
+
|
477
|
+
|
478
|
+
|
479
|
+
def resizeEvent(self, event):
|
480
|
+
|
481
|
+
if self.image.width() < self.width() or self.image.height() < self.height():
|
482
|
+
|
483
|
+
changeWidth = max(self.width(), self.image.width())
|
484
|
+
|
485
|
+
changeHeight = max(self.height(), self.image.height())
|
486
|
+
|
487
|
+
self.image = self.resizeImage(self.image, QSize(changeWidth, changeHeight))
|
488
|
+
|
489
|
+
self.update()
|
490
|
+
|
491
|
+
|
492
|
+
|
493
|
+
def resizeImage(self, image, newSize):
|
494
|
+
|
495
|
+
changeImage = QImage(newSize, QImage.Format_RGB32)
|
496
|
+
|
497
|
+
changeImage.fill(qRgb(255, 255, 255))
|
498
|
+
|
499
|
+
painter = QPainter(changeImage)
|
500
|
+
|
501
|
+
painter.drawImage(QPoint(0, 0), image)
|
502
|
+
|
503
|
+
return changeImage
|
504
|
+
|
505
|
+
|
506
|
+
|
507
|
+
def saveImage(self, filename):
|
508
|
+
|
509
|
+
if self.image.save(filename):
|
510
|
+
|
511
|
+
return True
|
512
|
+
|
513
|
+
else:
|
514
|
+
|
515
|
+
return False
|
516
|
+
|
517
|
+
|
518
|
+
|
519
|
+
def openImage(self, filename):
|
520
|
+
|
521
|
+
image = QImage()
|
522
|
+
|
523
|
+
if not image.load(filename):
|
524
|
+
|
525
|
+
return False
|
526
|
+
|
527
|
+
|
528
|
+
|
529
|
+
self.image = image
|
530
|
+
|
531
|
+
self.update()
|
532
|
+
|
533
|
+
return True
|
534
|
+
|
535
|
+
|
536
|
+
|
537
|
+
def penColor(self):
|
538
|
+
|
539
|
+
return self.myPenColor
|
540
|
+
|
541
|
+
|
542
|
+
|
543
|
+
def penWidth(self):
|
544
|
+
|
545
|
+
return self.myPenWidth
|
546
|
+
|
547
|
+
|
548
|
+
|
549
|
+
def setPenColor(self, newColor):
|
550
|
+
|
551
|
+
self.myPenColor = newColor
|
552
|
+
|
553
|
+
|
554
|
+
|
555
|
+
def setPenWidth(self, newWidth):
|
556
|
+
|
557
|
+
self.myPenWidth = newWidth
|
558
|
+
|
559
|
+
|
560
|
+
|
561
|
+
def resetImage(self):
|
562
|
+
|
563
|
+
self.image.fill(qRgb(255, 255, 255))
|
564
|
+
|
565
|
+
self.update()
|
566
|
+
|
567
|
+
|
568
|
+
|
569
|
+
def backImage(self):
|
570
|
+
|
571
|
+
if self.back:
|
572
|
+
|
573
|
+
back_ = self.back.pop()
|
574
|
+
|
575
|
+
#self.next.append(back_)
|
576
|
+
|
577
|
+
self.image = QImage(back_)
|
578
|
+
|
579
|
+
self.update()
|
580
|
+
|
581
|
+
|
582
|
+
|
583
|
+
# teratail 264079 modified by taizan-hokuto
|
584
|
+
|
585
|
+
class PredictCanvas(QWidget):
|
586
|
+
|
587
|
+
def __init__(self, parent = None):
|
588
|
+
|
589
|
+
super(PredictCanvas, self).__init__(parent)
|
590
|
+
|
591
|
+
|
592
|
+
|
593
|
+
self.image_predict = QImage()
|
594
|
+
|
595
|
+
self.image_predict.fill(qRgb(255, 0, 255))
|
596
|
+
|
597
|
+
|
598
|
+
|
599
|
+
def PredictImage(self, filename):
|
600
|
+
|
601
|
+
image = QImage()
|
602
|
+
|
603
|
+
if not image.load(filename):
|
604
|
+
|
605
|
+
return False
|
606
|
+
|
607
|
+
|
608
|
+
|
609
|
+
self.image_predict = image
|
610
|
+
|
611
|
+
self.update()
|
612
|
+
|
613
|
+
return True
|
614
|
+
|
615
|
+
|
616
|
+
|
617
|
+
def paintEvent(self, event):
|
618
|
+
|
619
|
+
painter = QPainter(self)
|
620
|
+
|
621
|
+
rect = event.rect()
|
622
|
+
|
57
623
|
painter.drawImage(rect, self.image_predict, rect)
|
58
624
|
|
59
|
-
|
60
|
-
|
61
|
-
```
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
③ 元コードでは、ToolFrame(QWidget)で新たにPredictCanvasが生成されてしまっており、メインウィジェットのpredict(predict_canvas)が共有されていません。
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
下記のように、メインウィジェットのpredict_canvasをToolFrame(QWidget)が受け取れるように修正する必要があります。
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
呼び出し元
|
74
|
-
|
75
|
-
```
|
76
|
-
|
77
|
-
def initUI(self):
|
78
|
-
|
79
|
-
frame = QWidget(self)
|
80
|
-
|
81
|
-
self.canvas = Canvas(frame)
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
self.predict_canvas = PredictCanvas(frame)
|
86
|
-
|
87
|
-
tools = ToolFrame(frame, canvas=self.predict_canvas) #修正部分 メインウィジェットのpredict_canvsを引数として呼び出す。
|
88
|
-
|
89
|
-
```
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
呼び出し先
|
94
|
-
|
95
|
-
```
|
96
|
-
|
97
|
-
class ToolFrame(QWidget):
|
98
|
-
|
99
|
-
def __init__(self, parent=None, canvas = None): 修正
|
100
|
-
|
101
|
-
super().__init__(parent)
|
102
|
-
|
103
|
-
self.predict = canvas # 修正 メインウィジェットのpredict_canvasを受け取る
|
104
|
-
|
105
|
-
self.initUI()
|
106
|
-
|
107
|
-
```
|
108
|
-
|
109
|
-
--------------------------
|
110
|
-
|
111
|
-
以上をまとめた全体コードが、下記のコードです。
|
112
|
-
|
113
|
-
```
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
import sys
|
118
|
-
|
119
|
-
from PyQt5.QtWidgets import (
|
120
|
-
|
121
|
-
QWidget, QApplication, QMainWindow, QAction,
|
122
|
-
|
123
|
-
QFileDialog, QColorDialog, QInputDialog, QPushButton,QHBoxLayout, QVBoxLayout, QApplication)
|
124
|
-
|
125
|
-
from PyQt5.QtGui import QPainter, QImage, QPen, qRgb
|
126
|
-
|
127
|
-
from PyQt5.QtCore import Qt, QPoint, QRect, QSize, QDir
|
128
|
-
|
129
|
-
from collections import deque
|
130
|
-
|
131
|
-
from PyQt5.QtCore import pyqtSlot
|
132
|
-
|
133
|
-
from PyQt5.QtGui import QIcon
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
class MainWindow(QMainWindow):
|
138
|
-
|
139
|
-
def __init__(self):
|
140
|
-
|
141
|
-
super(MainWindow, self).__init__()
|
142
|
-
|
143
|
-
self.setupUI()
|
144
|
-
|
145
|
-
self.setCentralWidget(self.initUI())
|
146
|
-
|
147
|
-
#クラスを呼び出す。
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
def initUI(self):
|
152
|
-
|
153
|
-
frame = QWidget(self)
|
154
|
-
|
155
|
-
self.canvas = Canvas(frame)
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
self.predict_canvas = PredictCanvas(frame)
|
160
|
-
|
161
|
-
tools = ToolFrame(frame, canvas=self.predict_canvas)
|
162
|
-
|
163
|
-
hbox = QHBoxLayout(frame)
|
164
|
-
|
165
|
-
#vbox=QVBoxLayout()
|
166
|
-
|
167
|
-
#vbox.addStretch(0)
|
168
|
-
|
169
|
-
#vbox.addWidget(tools)
|
170
|
-
|
171
|
-
#vbox.addWidget(predict)
|
172
|
-
|
173
|
-
hbox.addWidget(self.canvas)
|
174
|
-
|
175
|
-
hbox.addWidget(self.predict_canvas)
|
176
|
-
|
177
|
-
hbox.addWidget(tools)
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
frame.setLayout(hbox)
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
return frame
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
def setupUI(self):
|
190
|
-
|
191
|
-
menubar = self.menuBar()
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
openAct = QAction('&Open', self)
|
196
|
-
|
197
|
-
openAct.setShortcut('Ctrl+O')
|
198
|
-
|
199
|
-
openAct.triggered.connect(self.openFile)
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
exitAct = QAction('&Exit', self)
|
204
|
-
|
205
|
-
exitAct.setShortcut('Ctrl+Q')
|
206
|
-
|
207
|
-
exitAct.triggered.connect(self.close)
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
saveAct = QAction('&Save', self)
|
212
|
-
|
213
|
-
saveAct.setShortcut('Ctrl+S')
|
214
|
-
|
215
|
-
saveAct.triggered.connect(self.saveFile)
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
fileMenu = menubar.addMenu('&File')
|
220
|
-
|
221
|
-
#fileMenu.addAction(resetAct)
|
222
|
-
|
223
|
-
fileMenu.addAction(openAct)
|
224
|
-
|
225
|
-
fileMenu.addAction(saveAct)
|
226
|
-
|
227
|
-
fileMenu.addAction(exitAct)
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
# 画像付き Pencolor selectするアクションオブジェクト作成
|
232
|
-
|
233
|
-
selectColorAction = QAction( QIcon('sample/white-bear.png'),'Pen Color', self)
|
234
|
-
|
235
|
-
selectColorAction.setShortcut('Ctrl+A')
|
236
|
-
|
237
|
-
selectColorAction.triggered.connect(self.selectColor)
|
238
|
-
|
239
|
-
self.toolbar = self.addToolBar('Pen Color')
|
240
|
-
|
241
|
-
self.toolbar.addAction(selectColorAction)
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
# ツールバー作成
|
246
|
-
|
247
|
-
selectwidthAction = QAction( QIcon('sample/white-bear.png'),'Selecwidth', self)
|
248
|
-
|
249
|
-
selectwidthAction.triggered.connect(self.selectWidth)
|
250
|
-
|
251
|
-
self.toolbar = self.addToolBar('Width')
|
252
|
-
|
253
|
-
self.toolbar.addAction(selectwidthAction)
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
# ツールバー作成
|
258
|
-
|
259
|
-
backAction = QAction( QIcon('sample/white-bear.png'),'Back', self)
|
260
|
-
|
261
|
-
backAction.setShortcut('Ctrl+Z')
|
262
|
-
|
263
|
-
backAction.triggered.connect(self.on_back)
|
264
|
-
|
265
|
-
self.toolbar = self.addToolBar('Undo')
|
266
|
-
|
267
|
-
self.toolbar.addAction(backAction)
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
# ツールバー作成
|
272
|
-
|
273
|
-
clearAction = QAction( QIcon('sample/white-bear.png'),'Clear', self)
|
274
|
-
|
275
|
-
clearAction.setShortcut('Ctrl+B')
|
276
|
-
|
277
|
-
clearAction.triggered.connect(self.on_reset)
|
278
|
-
|
279
|
-
self.toolbar = self.addToolBar('Undo')
|
280
|
-
|
281
|
-
self.toolbar.addAction(clearAction)
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
@pyqtSlot()
|
286
|
-
|
287
|
-
def on_back(self):
|
288
|
-
|
289
|
-
print('PyQt5 button click')
|
290
|
-
|
291
|
-
self.canvas.backImage()
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
@pyqtSlot()
|
296
|
-
|
297
|
-
def on_reset(self):
|
298
|
-
|
299
|
-
self.canvas.resetImage()
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
@pyqtSlot()
|
304
|
-
|
305
|
-
def on_run(self):
|
306
|
-
|
307
|
-
fileName, _ = QFileDialog.getOpenFileName(self, "Open File", QDir.currentPath())
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
if fileName:
|
312
|
-
|
313
|
-
self.predict_canvas.PredictImage(fileName)
|
314
|
-
|
315
|
-
#self.canvas.openImage(fileName)
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
def selectColor(self):
|
320
|
-
|
321
|
-
newColor = QColorDialog.getColor(self.canvas.penColor())
|
322
|
-
|
323
|
-
self.canvas.setPenColor(newColor)
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
def selectWidth(self):
|
328
|
-
|
329
|
-
newWidth, ok = QInputDialog.getInt(
|
330
|
-
|
331
|
-
self, "select",
|
332
|
-
|
333
|
-
"select pen width: ", self.canvas.penWidth(), 1, 100, 1)
|
334
|
-
|
335
|
-
if ok:
|
336
|
-
|
337
|
-
self.canvas.setPenWidth(newWidth)
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
def openFile(self):
|
342
|
-
|
343
|
-
fileName, _ = QFileDialog.getOpenFileName(self, "Open File", QDir.currentPath())
|
344
|
-
|
345
|
-
if fileName:
|
346
|
-
|
347
|
-
self.predict_canvas.PredictImage(fileName)
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
def saveFile(self):
|
352
|
-
|
353
|
-
path = QDir.currentPath()
|
354
|
-
|
355
|
-
print(path)
|
356
|
-
|
357
|
-
fileName, _ = QFileDialog.getSaveFileName(self, "Save as",path)
|
358
|
-
|
359
|
-
fileName=fileName+'.png'
|
360
|
-
|
361
|
-
if fileName:
|
362
|
-
|
363
|
-
print(fileName)
|
364
|
-
|
365
|
-
return self.canvas.saveImage(fileName)
|
366
|
-
|
367
|
-
else:
|
368
|
-
|
369
|
-
print("you couldnt save the file")
|
370
|
-
|
371
|
-
return False
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
class ToolFrame(QWidget):
|
376
|
-
|
377
|
-
def __init__(self, parent=None, canvas = None):
|
378
|
-
|
379
|
-
super().__init__(parent)
|
380
|
-
|
381
|
-
self.predict = canvas # PredictCanvas()
|
382
|
-
|
383
|
-
self.initUI()
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
def initUI(self):
|
388
|
-
|
389
|
-
button_predict = QPushButton('predict', self)
|
390
|
-
|
391
|
-
button_predict.clicked.connect(self.on_run)
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
button_reset = QPushButton('Reset', self)
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
vbox = QVBoxLayout(self)
|
402
|
-
|
403
|
-
vbox.addStretch(1)
|
404
|
-
|
405
|
-
vbox.addWidget(button_predict)
|
406
|
-
|
407
|
-
vbox.addWidget(button_reset)
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
@pyqtSlot()
|
412
|
-
|
413
|
-
def on_run(self):
|
414
|
-
|
415
|
-
fileName, _ = QFileDialog.getOpenFileName(self, "Open File", QDir.currentPath())
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
if fileName:
|
420
|
-
|
421
|
-
self.predict.PredictImage(fileName)
|
422
|
-
|
423
|
-
#self.canvas.openImage(fileName)
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
class Canvas(QWidget):
|
428
|
-
|
429
|
-
def __init__(self, parent = None):
|
430
|
-
|
431
|
-
super(Canvas, self).__init__(parent)
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
self.myPenWidth = 2
|
436
|
-
|
437
|
-
self.myPenColor = Qt.black
|
438
|
-
|
439
|
-
self.image = QImage()
|
440
|
-
|
441
|
-
self.check = False
|
442
|
-
|
443
|
-
self.back = deque(maxlen = 10)
|
444
|
-
|
445
|
-
self.image = QImage(300, 300, QImage.Format_RGB32)
|
446
|
-
|
447
|
-
self.image.fill(qRgb(255, 255, 255))
|
448
|
-
|
449
|
-
#self.next = deque(maxlen = 10)
|
450
|
-
|
451
|
-
# initUIはもう必要ないから消しておこうね
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
def mousePressEvent(self, event):
|
458
|
-
|
459
|
-
if event.button() == Qt.LeftButton:
|
460
|
-
|
461
|
-
self.back.append(self.resizeImage(self.image, self.image.size()))
|
462
|
-
|
463
|
-
self.lastPos = event.pos()
|
464
|
-
|
465
|
-
self.check = True
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
def mouseMoveEvent(self, event):
|
470
|
-
|
471
|
-
if event.buttons() and Qt.LeftButton and self.check:
|
472
|
-
|
473
|
-
self.drawLine(event.pos())
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
def mouseReleaseEvent(self, event):
|
478
|
-
|
479
|
-
if event.button() == Qt.LeftButton and self.check:
|
480
|
-
|
481
|
-
self.drawLine(event.pos())
|
482
|
-
|
483
|
-
self.check = False
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
def drawLine(self, endPos):
|
488
|
-
|
489
|
-
painter = QPainter(self.image)
|
490
|
-
|
491
|
-
painter.setPen(
|
492
|
-
|
493
|
-
QPen(self.myPenColor, self.myPenWidth, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin)
|
494
|
-
|
495
|
-
)
|
496
|
-
|
497
|
-
painter.drawLine(self.lastPos, endPos)
|
498
|
-
|
499
|
-
self.update()
|
500
|
-
|
501
|
-
self.lastPos = QPoint(endPos)
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
def paintEvent(self, event):
|
506
|
-
|
507
|
-
painter = QPainter(self)
|
508
|
-
|
509
|
-
rect = event.rect()
|
510
|
-
|
511
|
-
painter.drawImage(rect, self.image, rect)
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
def resizeEvent(self, event):
|
516
|
-
|
517
|
-
if self.image.width() < self.width() or self.image.height() < self.height():
|
518
|
-
|
519
|
-
changeWidth = max(self.width(), self.image.width())
|
520
|
-
|
521
|
-
changeHeight = max(self.height(), self.image.height())
|
522
|
-
|
523
|
-
self.image = self.resizeImage(self.image, QSize(changeWidth, changeHeight))
|
524
|
-
|
525
|
-
self.update()
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
def resizeImage(self, image, newSize):
|
530
|
-
|
531
|
-
changeImage = QImage(newSize, QImage.Format_RGB32)
|
532
|
-
|
533
|
-
changeImage.fill(qRgb(255, 255, 255))
|
534
|
-
|
535
|
-
painter = QPainter(changeImage)
|
536
|
-
|
537
|
-
painter.drawImage(QPoint(0, 0), image)
|
538
|
-
|
539
|
-
return changeImage
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
def saveImage(self, filename):
|
544
|
-
|
545
|
-
if self.image.save(filename):
|
546
|
-
|
547
|
-
return True
|
548
|
-
|
549
|
-
else:
|
550
|
-
|
551
|
-
return False
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
def openImage(self, filename):
|
556
|
-
|
557
|
-
image = QImage()
|
558
|
-
|
559
|
-
if not image.load(filename):
|
560
|
-
|
561
|
-
return False
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
self.image = image
|
566
|
-
|
567
|
-
self.update()
|
568
|
-
|
569
|
-
return True
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
def penColor(self):
|
574
|
-
|
575
|
-
return self.myPenColor
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
def penWidth(self):
|
580
|
-
|
581
|
-
return self.myPenWidth
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
def setPenColor(self, newColor):
|
586
|
-
|
587
|
-
self.myPenColor = newColor
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
def setPenWidth(self, newWidth):
|
592
|
-
|
593
|
-
self.myPenWidth = newWidth
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
def resetImage(self):
|
598
|
-
|
599
|
-
self.image.fill(qRgb(255, 255, 255))
|
600
|
-
|
601
|
-
self.update()
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
def backImage(self):
|
606
|
-
|
607
|
-
if self.back:
|
608
|
-
|
609
|
-
back_ = self.back.pop()
|
610
|
-
|
611
|
-
#self.next.append(back_)
|
612
|
-
|
613
|
-
self.image = QImage(back_)
|
614
|
-
|
615
|
-
self.update()
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
# teratail 264079 modified by taizan-hokuto
|
620
|
-
|
621
|
-
class PredictCanvas(QWidget):
|
622
|
-
|
623
|
-
def __init__(self, parent = None):
|
624
|
-
|
625
|
-
super(PredictCanvas, self).__init__(parent)
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
self.image_predict = QImage()
|
630
|
-
|
631
|
-
self.image_predict.fill(qRgb(255, 0, 255))
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
def PredictImage(self, filename):
|
636
|
-
|
637
|
-
image = QImage()
|
638
|
-
|
639
|
-
if not image.load(filename):
|
640
|
-
|
641
|
-
return False
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
self.image_predict = image
|
646
|
-
|
647
|
-
self.update()
|
648
|
-
|
649
|
-
return True
|
650
|
-
|
651
|
-
|
652
|
-
|
653
|
-
def paintEvent(self, event):
|
654
|
-
|
655
|
-
painter = QPainter(self)
|
656
|
-
|
657
|
-
rect = event.rect()
|
658
|
-
|
659
|
-
painter.drawImage(rect, self.image_predict, rect)
|
660
|
-
|
661
625
|
|
662
626
|
|
663
627
|
|
1
追記
test
CHANGED
@@ -1,12 +1,10 @@
|
|
1
|
+
※投稿後追記:上のteamiklさんの回答の方が正確なため、teamiklさんの回答&コードを参考にされた方がよいと思います。
|
2
|
+
|
3
|
+
|
4
|
+
|
1
|
-
|
5
|
+
修正してみました。
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
+
|
6
|
-
|
7
|
-
canvasの追加について、`frame = QWidget(self)` で定義されたframeが最終的にwidgetに配置されていません。
|
7
|
+
⓵ canvasの追加について、`frame = QWidget(self)` で定義されたframeが最終的にwidgetに配置されていません。
|
8
|
-
|
9
|
-
|
10
8
|
|
11
9
|
|
12
10
|
|
@@ -44,9 +42,7 @@
|
|
44
42
|
|
45
43
|
|
46
44
|
|
47
|
-
## ② PredictCanvasオブジェクトにおける再描画の欠如
|
48
|
-
|
49
|
-
PredictCanvasで下記のようにpaintEventを実装してあげないと、描画更新されません。
|
45
|
+
② PredictCanvasで下記のようにpaintEventを実装してあげないと、描画更新されません。
|
50
46
|
|
51
47
|
|
52
48
|
|
@@ -66,9 +62,7 @@
|
|
66
62
|
|
67
63
|
|
68
64
|
|
69
|
-
## ③ predict_canvasのコンテキストがオブジェクト間で共有されていない
|
70
|
-
|
71
|
-
元コードでは、ToolFrame(QWidget)で新たにPredictCanvasが生成されてしまっており、メインウィジェットのpredict(predict_canvas)が共有されていません。
|
65
|
+
③ 元コードでは、ToolFrame(QWidget)で新たにPredictCanvasが生成されてしまっており、メインウィジェットのpredict(predict_canvas)が共有されていません。
|
72
66
|
|
73
67
|
|
74
68
|
|
@@ -112,18 +106,8 @@
|
|
112
106
|
|
113
107
|
```
|
114
108
|
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
109
|
--------------------------
|
120
110
|
|
121
|
-
--------------------------
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
111
|
以上をまとめた全体コードが、下記のコードです。
|
128
112
|
|
129
113
|
```
|