-
Notifications
You must be signed in to change notification settings - Fork 0
/
qlabel_group_surface.py
656 lines (543 loc) · 26.7 KB
/
qlabel_group_surface.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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
# !/usr/bin/env python
# -*-coding:utf-8-*-
"""
DigiSun: a software to transform sunspot drawings into exploitable data. It allows to scan drawings, extract its information and store it in a database.
Copyright (C) 2019 Sabrina Bechet at Royal Observatory of Belgium (ROB)
This file is part of DigiSun.
DigiSun is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
DigiSun is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with DigiSun. If not, see <https://www.gnu.org/licenses/>.
"""
import sys
import math
import numpy as np
import analyse_mode_bool
from shapely.geometry import Point
from shapely.geometry.polygon import Polygon
from PyQt5 import QtGui, QtWidgets, QtCore
from PIL import Image, ImageQt
__author__ = "Mael Panouillot, Sabrina Bechet"
sys.setrecursionlimit(10000)
class GroupSurfaceWidget(QtWidgets.QWidget):
bigger_frame = QtCore.pyqtSignal()
smaller_frame = QtCore.pyqtSignal()
surface_saved = QtCore.pyqtSignal()
def __init__(self):
super(GroupSurfaceWidget, self).__init__()
self.layout = QtWidgets.QVBoxLayout()
self.layout.setSpacing(15)
self.setLayout(self.layout)
self.radius_division_factor = 400
self.scroll = QtWidgets.QScrollArea()
self.scroll\
.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
self.scroll\
.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
self.scroll.setWidgetResizable(True)
self.qlabel_group_surface = QLabelGroupSurface()
self.qlabel_group_surface.setAlignment(QtCore.Qt.AlignTop and
QtCore.Qt.AlignLeft)
self.scroll.setWidget(self.qlabel_group_surface)
self.radius = 0
self.start_x = 0
self.start_y = 0
self.center_x = 0
self.center_y = 0
qlabel_title = QtWidgets.QLabel("Area calculation")
qlabel_title.setAlignment(QtCore.Qt.AlignCenter)
qlabel_title.setContentsMargins(0, 5, 0, 5)
threshold_slider = QtWidgets.QSlider(QtCore.Qt.Horizontal)
threshold_slider.setRange(0, 256)
threshold_slider.setTickPosition(QtWidgets.QSlider.TicksBelow)
self.default_threshold = 225
threshold_slider.setValue(self.default_threshold)
threshold_slider.valueChanged.connect(
lambda: self.update_img_threshold_value(threshold_slider.value()))
self.threshold_linedit = QtWidgets.QLineEdit(str(self.default_threshold))
self.threshold_linedit.setDisabled(True)
self.threshold_linedit.setStyleSheet(
"background-color: lightgray; color:black")
threshold_layout = QtWidgets.QFormLayout()
threshold_layout.addRow("Threshold selection:",
self.threshold_linedit)
qlabel_general = QtWidgets.QLabel("General tools:")
button_size = 50
zoom_in_but = QtWidgets.QToolButton()
zoom_in_but.setToolTip("zoom in")
zoom_in_but.setIcon(QtGui.QIcon('icons/Smashicons/zoom-in.png'))
zoom_in_but.clicked.connect(
lambda: self.qlabel_group_surface.zoom_in(2.))
zoom_in_but.setMinimumWidth(button_size)
zoom_in_but.setShortcut(QtGui.QKeySequence("Ctrl++"))
zoom_out_but = QtWidgets.QToolButton()
zoom_out_but.setToolTip("zoom out'")
zoom_out_but.setIcon(QtGui.QIcon('icons/Smashicons/zoom-out.png'))
zoom_out_but.clicked.connect(
lambda: self.qlabel_group_surface.zoom_in(1/2.))
zoom_out_but.setMinimumWidth(button_size)
zoom_out_but.setShortcut(QtGui.QKeySequence("Ctrl+-"))
reset_but = QtWidgets.QToolButton()
reset_but.setToolTip("reset'")
reset_but.setIcon(QtGui.QIcon('icons/Pixel_perfect/settings.png'))
reset_but.clicked.connect(
lambda: self.update_img_threshold_value(threshold_slider.value()))
reset_but.setMinimumWidth(button_size)
reset_but.setShortcut(QtGui.QKeySequence("Ctrl+z"))
bigger_frame_but = QtWidgets.QToolButton()
bigger_frame_but.setToolTip("expand frame")
bigger_frame_but.setIcon(
QtGui.QIcon('icons/Freepik/thin-expand-arrows.png'))
bigger_frame_but.clicked.connect(
lambda: self.bigger_frame.emit())
bigger_frame_but.setMinimumWidth(button_size)
bigger_frame_but.setShortcut(QtGui.QKeySequence("Ctrl+e"))
smaller_frame_but = QtWidgets.QToolButton()
smaller_frame_but.setToolTip("reduce frame")
smaller_frame_but.setIcon(
QtGui.QIcon('icons/Freepik/exit-full-screen-arrows.png'))
smaller_frame_but.clicked.connect(
lambda: self.smaller_frame.emit())
smaller_frame_but.setMinimumWidth(button_size)
smaller_frame_but.setShortcut(QtGui.QKeySequence("Ctrl+r"))
draw_polygon_but = QtWidgets.QToolButton()
draw_polygon_but.setToolTip("draw polygon")
draw_polygon_but.setIcon(
QtGui.QIcon('icons/Darrio_Ferrando/polygon.png'))
draw_polygon_but.clicked.connect(
lambda: self.set_opposite_value(
self.qlabel_group_surface.polygon_mode))
draw_polygon_but.clicked.connect(
self.qlabel_group_surface.draw_polygon)
draw_polygon_but.setMinimumWidth(button_size)
draw_polygon_but.setShortcut(QtGui.QKeySequence("Ctrl+d"))
cut_polygon_but = QtWidgets.QToolButton()
cut_polygon_but.setToolTip("cut polygon")
cut_polygon_but.setIcon(
QtGui.QIcon('icons/Freepik/crop.png'))
cut_polygon_but.clicked.connect(
lambda: self.cut_polygon(threshold_slider.value()))
cut_polygon_but.setMinimumWidth(button_size)
cut_polygon_but.setShortcut(QtGui.QKeySequence("Ctrl+c"))
draw_1pixel_black_but = QtWidgets.QToolButton()
draw_1pixel_black_but.setToolTip("draw black pixel")
draw_1pixel_black_but.setIcon(
QtGui.QIcon('icons/Freepik/1pix_black_square.png'))
draw_1pixel_black_but.clicked.connect(
lambda: self.set_opposite_value(
self.qlabel_group_surface.black_pencil_mode))
draw_1pixel_black_but.clicked.connect(lambda: self.draw_pencil(0))
draw_1pixel_black_but.setMinimumWidth(button_size)
draw_1pixel_black_but.setShortcut(QtGui.QKeySequence("Ctrl+x"))
draw_1pixel_white_but = QtWidgets.QToolButton()
draw_1pixel_white_but.setToolTip("draw white pixel")
draw_1pixel_white_but.setIcon(
QtGui.QIcon('icons/Freepik/1pix_white_square.png'))
draw_1pixel_white_but.clicked.connect(
lambda: self.set_opposite_value(
self.qlabel_group_surface.white_pencil_mode))
draw_1pixel_white_but.clicked.connect(lambda: self.draw_pencil(255))
draw_1pixel_white_but.setMinimumWidth(button_size)
draw_1pixel_white_but.setShortcut(QtGui.QKeySequence("Ctrl+w"))
bucket_white_fill_but = QtWidgets.QToolButton()
bucket_white_fill_but.setToolTip("white bucket fill")
bucket_white_fill_but.setIcon(
QtGui.QIcon('icons/Freepik/white-bucket.png'))
bucket_white_fill_but.clicked.connect(
lambda: self.set_opposite_value(
self.qlabel_group_surface.white_bucket_mode))
bucket_white_fill_but.clicked.connect(
lambda: self.qlabel_group_surface.set_bucket_fill(
self.current_array,
0,
255))
bucket_white_fill_but.setMinimumWidth(button_size)
bucket_white_fill_but.setShortcut(QtGui.QKeySequence("alt+w"))
bucket_black_fill_but = QtWidgets.QToolButton()
bucket_black_fill_but.setToolTip("black bucket fill" )
bucket_black_fill_but.setIcon(
QtGui.QIcon('icons/Freepik/black-bucket.png'))
bucket_black_fill_but.clicked.connect(
lambda: self.set_opposite_value(
self.qlabel_group_surface.black_bucket_mode))
bucket_black_fill_but.clicked.connect(
lambda: self.qlabel_group_surface.set_bucket_fill(
self.current_array,
255,
0))
bucket_black_fill_but.setMinimumWidth(button_size)
bucket_black_fill_but.setShortcut(QtGui.QKeySequence("alt+x"))
save_but = QtWidgets.QPushButton("sa&ve area")
save_but.setToolTip("\'Ctrl+v\'")
save_but.setShortcut(QtGui.QKeySequence("Ctrl+v"))
save_but.clicked.connect(self.fill_surface_info)
pixel_slider = QtWidgets.QSlider(QtCore.Qt.Horizontal)
pixel_slider.setRange(0, 2)
pixel_slider.setTickInterval(1)
pixel_slider.setTickPosition(QtWidgets.QSlider.TicksBelow)
self.pixel_linedit = QtWidgets.QLineEdit(str(1))
self.pixel_linedit.setDisabled(True)
self.pixel_linedit.setStyleSheet(
"background-color: lightgray; color:black")
pencil_size_layout = QtWidgets.QFormLayout()
pencil_size_layout.addRow("Pencil size (pixel):",
self.pixel_linedit)
pixel_slider.valueChanged.connect(
lambda: self.qlabel_group_surface.update_pixel_size(
pixel_slider.value()))
pixel_slider.valueChanged.connect(
lambda: self.pixel_linedit.setText(str((pixel_slider.value() * 2) + 1)))
self.qlabel_group_surface.polygon_mode.value_changed.connect(
lambda: self.set_button_color(
self.qlabel_group_surface.polygon_mode.value,
draw_polygon_but))
if self.qlabel_group_surface.polygon_mode.value:
draw_polygon_but.setStyleSheet("background-color: lightblue")
self.qlabel_group_surface.white_pencil_mode.value_changed.connect(
lambda: self.set_button_color(
self.qlabel_group_surface.white_pencil_mode.value,
draw_1pixel_white_but))
if self.qlabel_group_surface.white_pencil_mode.value:
draw_1pixel_white_but.setStyleSheet("background-color: lightblue")
self.qlabel_group_surface.black_pencil_mode.value_changed.connect(
lambda: self.set_button_color(
self.qlabel_group_surface.black_pencil_mode.value,
draw_1pixel_black_but))
if self.qlabel_group_surface.black_pencil_mode.value:
draw_1pixel_black_but.setStyleSheet("background-color: lightblue")
self.qlabel_group_surface.white_bucket_mode.value_changed.connect(
lambda: self.set_button_color(
self.qlabel_group_surface.white_bucket_mode.value,
bucket_white_fill_but))
if self.qlabel_group_surface.white_bucket_mode.value:
bucket_white_fill_but.setStyleSheet("background-color: lightblue")
self.qlabel_group_surface.black_bucket_mode.value_changed.connect(
lambda: self.set_button_color(
self.qlabel_group_surface.black_bucket_mode.value,
bucket_black_fill_but))
if self.qlabel_group_surface.black_bucket_mode.value:
bucket_black_fill_but.setStyleSheet("background-color: lightblue")
layout_general = QtWidgets.QGridLayout()
layout_general.addWidget(qlabel_general, 0, 0, 1, -1)
layout_general.addWidget(zoom_in_but, 1, 0)
layout_general.addWidget(zoom_out_but, 1, 1)
layout_general.addWidget(reset_but, 1, 2)
layout_general.addWidget(bigger_frame_but, 1, 3)
layout_general.addWidget(smaller_frame_but, 1, 4)
layout_general.addWidget(draw_polygon_but, 1, 5)
layout_general.addWidget(cut_polygon_but, 1, 6)
layout_general.addWidget(bucket_white_fill_but, 2, 0)
layout_general.addWidget(bucket_black_fill_but, 2, 1)
layout_general.addWidget(draw_1pixel_white_but, 2, 2)
layout_general.addWidget(draw_1pixel_black_but, 2, 3)
layout_general.addWidget(save_but, 2, 4, 1, -1)
layout_general.addLayout(pencil_size_layout, 3, 0, 1, 4)
layout_general.addWidget(pixel_slider, 3, 4, 1, 3)
form_layout = QtWidgets.QFormLayout()
self.pixel_number_linedit = QtWidgets.QLineEdit()
self.projected_surface_linedit = QtWidgets.QLineEdit()
self.deprojected_surface_linedit = QtWidgets.QLineEdit()
form_layout.addRow("Pixel Number:",
self.pixel_number_linedit)
form_layout.addRow("Projected area (msd):",
self.projected_surface_linedit)
form_layout.addRow("Deprojected area (msh):",
self.deprojected_surface_linedit)
self.layout.addWidget(qlabel_title)
self.layout.addLayout(threshold_layout)
self.layout.addWidget(threshold_slider)
self.layout.addLayout(layout_general)
#self.layout.addLayout(pencil_size_layout)
#self.layout.addWidget(pixel_slider)
self.layout.addWidget(self.scroll)
self.layout.addLayout(form_layout)
#self.layout.addWidget(save_but)
self.qlabel_group_surface.array_changed.connect(
lambda: self.set_img(self.qlabel_group_surface.array))
def set_opposite_value(self, mode):
if mode.value:
mode.value = False
else:
mode.value = True
def set_button_color(self, mode_bool, but):
if mode_bool is True:
but.setStyleSheet("background-color: lightblue")
elif mode_bool is False:
but.setStyleSheet("background-color: lightgray")
def draw_pencil(self, new_value):
self.qlabel_group_surface.set_pencil_array(
self.current_array, new_value)
def cut_polygon(self, value):
cut_array = self.qlabel_group_surface.cut_polygon(self.current_array)
self.set_img(cut_array)
def update_img_threshold_value(self, value):
self.qlabel_group_surface.polygon_mode.value = False
self.qlabel_group_surface.white_pencil_mode.value = False
self.qlabel_group_surface.black_pencil_mode.value = False
self.qlabel_group_surface.white_bucket_mode.value = False
self.qlabel_group_surface.black_bucket_mode.value = False
self.default_threshold = value
new_selection_thresh = self.threshold(value)
self.threshold_linedit.setText(str(value))
self.set_img(new_selection_thresh)
self.qlabel_group_surface.pointsList = []
def set_array(self, np_array):
#self.qlabel_group_surface.zoom_in(2.)
self.qlabel_group_surface.polygon_mode.value = False
self.qlabel_group_surface.white_pencil_mode.value = False
self.qlabel_group_surface.black_pencil_mode.value = False
self.qlabel_group_surface.white_bucket_mode.value = False
self.qlabel_group_surface.black_bucket_mode.value = False
self.selection_array = np_array
self.set_img(self.threshold(self.default_threshold))
self.qlabel_group_surface.pointsList = []
def threshold(self, value):
selection_array_thresh = np.copy(self.selection_array)
selection_array_thresh[np.where(selection_array_thresh>value)] = 0
selection_array_thresh[np.where(selection_array_thresh>0)] = 255
return selection_array_thresh
def set_img(self, img):
self.current_array = img
self.qlabel_group_surface.set_original_img(img)
self.nb_pixel = self.count_pixel(img)
self.projected_area = self.projected_area_calculation(self.nb_pixel)
self.deprojected_area = self.deprojected_area_calculation(img)
self.pixel_number_linedit.setText(str(self.nb_pixel))
self.projected_surface_linedit.setText(
'{0:.2f}'.format(self.projected_area))
self.deprojected_surface_linedit.setText(
'{0:.2f}'.format(self.deprojected_area))
def count_pixel(self, img):
return np.count_nonzero(img)
def fill_surface_info(self):
self.drawing.group_lst[self.index].set_surface(self.nb_pixel,
self.projected_area,
self.deprojected_area)
self.surface_saved.emit()
def set_group_info(self, drawing, index, start_x, start_y):
self.drawing = drawing
self.radius = drawing.calibrated_radius
self.start_x = start_x
self.start_y = start_y
self.center_x = drawing.calibrated_center.x
self.center_y = drawing.calibrated_center.y
self.index = index
def deprojected_area_calculation(self, img):
if self.radius:
index_x, index_y = img.nonzero()
pos_x = [x + self.start_x for x in index_x]
pos_y = [x + self.start_y for x in index_y]
deprojected_area_sum = 0
for i in range(len(pos_x)):
distance_from_center = math.sqrt(
(pos_x[i] - self.center_x)**2 +
(pos_y[i] - self.center_y)**2)
if distance_from_center < self.radius:
center_to_limb_angle = (math.asin(distance_from_center *
1./self.radius))
deprojected_area_sum += 1./math.cos(center_to_limb_angle)
return (deprojected_area_sum * math.pow(10, 6) /
(2 * math.pi * self.radius**2))
else:
return 0
def projected_area_calculation(self, nb_pixel):
if self.radius:
return nb_pixel * math.pow(10, 6)/(math.pi * self.radius**2)
else:
return 0
def update_frame_surface(self, radius, step=0):
"""
change the size of the frame around the surface selection.
"""
div_factor = self.radius_division_factor
div_factor_tmp = div_factor + step * 100
if div_factor_tmp:
frame_size_tmp = math.floor(radius / div_factor_tmp) * 100
if frame_size_tmp > 0:
frame_size = frame_size_tmp
self.radius_division_factor = div_factor_tmp
else:
frame_size = math.floor(radius / self.radius_division_factor) * 100
return frame_size
class QLabelGroupSurface(QtWidgets.QLabel):
array_changed = QtCore.pyqtSignal()
def __init__(self):
super(QLabelGroupSurface, self).__init__()
# used for the reset
self.original_pixmap = QtGui.QPixmap()
# used for the polygon drawing
self.first_pixamp_polygon = QtGui.QPixmap()
self.width_scale = 300
self.height_scale = 300
self.scaling_factor = 1
self.pointsList = []
self.polygon_mode = analyse_mode_bool.analyseModeBool(False)
self.white_pencil_mode = analyse_mode_bool.analyseModeBool(False)
self.black_pencil_mode = analyse_mode_bool.analyseModeBool(False)
self.white_bucket_mode = analyse_mode_bool.analyseModeBool(False)
self.black_bucket_mode = analyse_mode_bool.analyseModeBool(False)
self.max_count = 0
self.pixel_size = 0
def zoom_in(self, scaling_factor):
self.width_scale *= scaling_factor
self.height_scale *= scaling_factor
self.scaling_factor *= scaling_factor
bis = self.original_pixmap.scaled(int(self.width_scale),
int(self.height_scale),
QtCore.Qt.KeepAspectRatio)
self.setPixmap(bis)
def set_original_img(self, np_array):
self.original_pixmap = self.np2qpixmap(np_array).copy()
self.pointsList = []
bis = self.original_pixmap.scaled(int(self.width_scale),
int(self.height_scale),
QtCore.Qt.KeepAspectRatio)
self.setPixmap(bis)
def np2qpixmap(self, np_img):
"""convert np array into pixmap"""
im8 = Image.fromarray(np_img)
imQt = QtGui.QImage(ImageQt.ImageQt(im8))
return QtGui.QPixmap.fromImage(imQt)
def draw_polygon(self, position=None):
"""
Method that has a *graphic* role. it shows the polygon on screen and
records the list of points.
"""
self.white_pencil_mode.value = False
self.black_pencil_mode.value = False
self.white_bucket_mode.value = False
self.black_bucket_mode.value = False
painter = QtGui.QPainter()
pen = QtGui.QPen(QtCore.Qt.red)
pen.setWidth(5)
if position:
self.pointsList.append(position)
if len(self.pointsList) == 0:
self.first_pixamp_polygon = self.pixmap().copy()
bis = self.first_pixamp_polygon.scaled(int(self.width_scale),
int(self.height_scale),
QtCore.Qt.KeepAspectRatio)
self.setPixmap(bis)
painter.begin(self.pixmap())
pen_polygon = QtGui.QPen(QtCore.Qt.cyan)
pen_polygon.setWidth(3)
painter.setPen(pen_polygon)
if len(self.pointsList) == 1:
painter.drawPoint(self.pointsList[-1])
else:
for i in range(len(self.pointsList)):
painter.drawLine(self.pointsList[i-1], self.pointsList[i])
painter.end()
def cut_polygon(self, array):
"""
Change the matrix of pixel and return a new array.
"""
self.polygon_mode.value = False
self.white_pencil_mode.value = False
self.black_pencil_mode.value = False
self.white_bucket_mode.value = False
self.black_bucket_mode.value = False
polygon_points = [(int(math.floor(a.y() * array.shape[1] /
self.height_scale)),
int(math.floor(a.x() * array.shape[0] /
self.width_scale)))
for a in self.pointsList]
polygon = Polygon(polygon_points)
count = 0
for index, value in np.ndenumerate(array):
if value > 0:
count += 1
if polygon.contains(Point(index)) is False:
array[index] = 0
return array
def set_pencil_array(self, array, new_value):
self.polygon_mode.value = False
if new_value == 0:
self.white_pencil_mode.value = False
else:
self.black_pencil_mode.value = False
self.white_bucket_mode.value = False
self.black_bucket_mode.value = False
self.array = array
self.new_value = new_value
def set_bucket_fill(self, array, old_value, new_value):
self.polygon_mode.value = False
self.white_pencil_mode.value = False
self.black_pencil_mode.value = False
if new_value == 0:
self.white_bucket_mode.value = False
else:
self.black_bucket_mode.value = False
self.array = array
self.new_value = new_value
self.old_value = old_value
def bucket_fill(self, position_x, position_y, count=0):
try:
self.max_count = max(self.max_count, count)
if self.array[position_x, position_y] == self.new_value:
return
self.array[position_x, position_y] = self.new_value
if position_x > 0:
self.bucket_fill(position_x - 1, position_y, count+1)
if position_x < self.array.shape[1] - 1:
self.bucket_fill(position_x + 1, position_y, count+1)
if position_y > 0:
self.bucket_fill(position_x, position_y - 1, count+1)
if position_y < self.array.shape[0] - 1:
self.bucket_fill(position_x, position_y + 1, count+1)
except RuntimeError:
print("Time too long, check that the border is closed!")
def update_pixel_size(self, pixel_size):
self.pixel_size = pixel_size
def mousePressEvent(self, QMouseEvent):
position = QMouseEvent.pos()
if (self.white_pencil_mode.value or self.black_pencil_mode.value or
self.white_bucket_mode.value or self.black_bucket_mode.value):
pos_y_scaled = int(math.floor(position.y() * self.array.shape[1] /
self.height_scale))
pos_x_scaled = int(math.floor(position.x() * self.array.shape[0] /
self.width_scale))
y_min = pos_y_scaled - self.pixel_size
y_max = pos_y_scaled + self.pixel_size + 1
x_min = pos_x_scaled - self.pixel_size
x_max = pos_x_scaled + self.pixel_size + 1
if self.polygon_mode.value:
self.draw_polygon(position)
elif self.white_pencil_mode.value or self.black_pencil_mode.value:
self.array[y_min:y_max, x_min:x_max] = self.new_value
self.array_changed.emit()
elif self.white_bucket_mode.value or self.black_bucket_mode.value:
self.bucket_fill(pos_y_scaled, pos_x_scaled)
self.array_changed.emit()
def mouseMoveEvent(self, QMouseEvent):
if self.white_pencil_mode.value or self.black_pencil_mode.value:
position = QMouseEvent.pos()
pos_y_scaled = int(math.floor(position.y() * self.array.shape[1] /
self.height_scale))
pos_x_scaled = int(math.floor(position.x() * self.array.shape[0] /
self.width_scale))
y_min = pos_y_scaled - self.pixel_size
y_max = pos_y_scaled + self.pixel_size + 1
x_min = pos_x_scaled - self.pixel_size
x_max = pos_x_scaled + self.pixel_size + 1
self.array[y_min:y_max, x_min:x_max] = self.new_value
self.array_changed.emit()
def mouseReleaseEvent(self, QMouseEvent):
if self.white_pencil_mode.value or self.black_pencil_mode.value:
position = QMouseEvent.pos()
pos_y_scaled = int(math.floor(position.y() * self.array.shape[1] /
self.height_scale))
pos_x_scaled = int(math.floor(position.x() * self.array.shape[0] /
self.width_scale))
y_min = pos_y_scaled - self.pixel_size
y_max = pos_y_scaled + self.pixel_size + 1
x_min = pos_x_scaled - self.pixel_size
x_max = pos_x_scaled + self.pixel_size + 1
self.array[y_min:y_max, x_min:x_max] = self.new_value
self.array_changed.emit()