-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprototype.py
More file actions
792 lines (576 loc) · 24.7 KB
/
prototype.py
File metadata and controls
792 lines (576 loc) · 24.7 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
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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
import sys
import ntpath
from PyQt5 import QtWidgets
from PyQt5.QtCore import Qt
# from PyQt5.QtGui import *
from PyQt5.QtGui import QFont, QPixmap
from PyQt5.QtWidgets import QWidget,QApplication, QTabWidget, QPushButton, QLabel, QHBoxLayout, QLineEdit, QCheckBox, QSlider, QFileDialog, QDesktopWidget, QMessageBox, QFrame, QVBoxLayout
from parser import IPDSL
from functools import partial
from img_modifier import img_helper
from img_modifier import color_filter
from PIL import ImageQt
from logging.config import fileConfig
import logging
logger = logging.getLogger()
# original img, can't be modified
_img_original = None
_img_preview = None
original_img_path = None
# constants
THUMB_BORDER_COLOR_ACTIVE = "#3893F4"
THUMB_BORDER_COLOR = "#ccc"
BTN_MIN_WIDTH = 120
ROTATION_BTN_SIZE = (70, 30)
THUMB_SIZE = 120
SLIDER_MIN_VAL = -100
SLIDER_MAX_VAL = 100
SLIDER_DEF_VAL = 0
class Operations:
def __init__(self):
self.color_filter = None
self.flip_left = False
self.flip_top = False
self.rotation_angle = 0
self.size = None
self.brightness = 0
self.sharpness = 0
self.contrast = 0
def reset(self):
self.color_filter = None
self.brightness = 0
self.sharpness = 0
self.contrast = 0
self.size = None
self.flip_left = False
self.flip_top = False
self.rotation_angle = 0
def has_changes(self):
return self.color_filter or self.flip_left\
or self.flip_top or self.rotation_angle\
or self.contrast or self.brightness\
or self.sharpness or self.size
def __str__(self):
return f"size: {self.size}, filter: {self.color_filter}, " \
f"b: {self.brightness} c: {self.contrast} s: {self.sharpness}, " \
f"flip-left: {self.flip_left} flip-top: {self.flip_top} rotation: {self.rotation_angle}"
operations = Operations()
def _get_ratio_height(width, height, r_width):
return int(r_width/width*height)
def _get_ratio_width(width, height, r_height):
return int(r_height/height*width)
def _get_converted_point(user_p1, user_p2, p1, p2, x):
"""
convert user ui slider selected value (x) to PIL value
user ui slider scale is -100 to 100, PIL scale is -1 to 2
example:
- user selected 50
- PIL value is 1.25
"""
r = (x - user_p1) / (user_p2 - user_p1)
return p1 + r * (p2 - p1)
def _get_img_with_all_operations():
logger.debug(operations)
b = operations.brightness
c = operations.contrast
s = operations.sharpness
img = _img_preview
if b != 0:
img = img_helper.brightness(img, b)
if c != 0:
img = img_helper.contrast(img, c)
if s != 0:
img = img_helper.sharpness(img, s)
if operations.rotation_angle:
img = img_helper.rotate(img, operations.rotation_angle)
if operations.flip_left:
img = img_helper.flip_left(img)
if operations.flip_top:
img = img_helper.flip_top(img)
if operations.size:
img = img_helper.resize(img, *operations.size)
return img
class ActionTabs(QTabWidget):
"""Action tabs widget"""
def __init__(self, parent):
super().__init__()
self.parent = parent
self.filters_tab = FiltersTab(self)
self.adjustment_tab = AdjustingTab(self)
self.modification_tab = ModificationTab(self)
self.rotation_tab = RotationTab(self)
self.commandTab = CommandTab(self)
self.addTab(self.commandTab, "Command")
self.addTab(self.filters_tab, "Filters")
self.addTab(self.adjustment_tab, "Adjusting")
self.addTab(self.modification_tab, "Modification")
self.addTab(self.rotation_tab, "Rotation")
self.setMaximumHeight(190)
class RotationTab(QWidget):
"""Rotation tab widget"""
def __init__(self, parent):
super().__init__()
self.parent = parent
rotate_left_btn = QPushButton("↺ 90°")
rotate_left_btn.setMinimumSize(*ROTATION_BTN_SIZE)
rotate_left_btn.clicked.connect(self.on_rotate_left)
rotate_right_btn = QPushButton("↻ 90°")
rotate_right_btn.setMinimumSize(*ROTATION_BTN_SIZE)
rotate_right_btn.clicked.connect(self.on_rotate_right)
flip_left_btn = QPushButton("⇆")
flip_left_btn.setMinimumSize(*ROTATION_BTN_SIZE)
flip_left_btn.clicked.connect(self.on_flip_left)
flip_top_btn = QPushButton("↑↓")
flip_top_btn.setMinimumSize(*ROTATION_BTN_SIZE)
flip_top_btn.clicked.connect(self.on_flip_top)
rotate_lbl = QLabel("Rotate")
rotate_lbl.setAlignment(Qt.AlignCenter)
rotate_lbl.setFixedWidth(140)
flip_lbl = QLabel("Flip")
flip_lbl.setAlignment(Qt.AlignCenter)
flip_lbl.setFixedWidth(140)
lbl_layout = QHBoxLayout()
lbl_layout.setAlignment(Qt.AlignCenter)
lbl_layout.addWidget(rotate_lbl)
lbl_layout.addWidget(flip_lbl)
btn_layout = QHBoxLayout()
btn_layout.setAlignment(Qt.AlignCenter)
btn_layout.addWidget(rotate_left_btn)
btn_layout.addWidget(rotate_right_btn)
btn_layout.addWidget(QVLine())
btn_layout.addWidget(flip_left_btn)
btn_layout.addWidget(flip_top_btn)
main_layout = QVBoxLayout()
main_layout.setAlignment(Qt.AlignCenter)
main_layout.addLayout(lbl_layout)
main_layout.addLayout(btn_layout)
self.setLayout(main_layout)
def on_rotate_left(self):
logger.debug("rotate left")
operations.rotation_angle = 0 if operations.rotation_angle == 270 else operations.rotation_angle + 90
self.parent.parent.place_preview_img()
def on_rotate_right(self):
logger.debug("rotate left")
operations.rotation_angle = 0 if operations.rotation_angle == -270 else operations.rotation_angle - 90
self.parent.parent.place_preview_img()
def on_flip_left(self):
logger.debug("flip left-right")
operations.flip_left = not operations.flip_left
self.parent.parent.place_preview_img()
def on_flip_top(self):
logger.debug("flip top-bottom")
operations.flip_top = not operations.flip_top
self.parent.parent.place_preview_img()
class ModificationTab(QWidget):
"""Modification tab widget"""
def __init__(self, parent):
super().__init__()
self.parent = parent
self.width_lbl = QLabel('width:', self)
self.width_lbl.setFixedWidth(100)
self.height_lbl = QLabel('height:', self)
self.height_lbl.setFixedWidth(100)
self.width_box = QLineEdit(self)
self.width_box.textEdited.connect(self.on_width_change)
self.width_box.setMaximumWidth(100)
self.height_box = QLineEdit(self)
self.height_box.textEdited.connect(self.on_height_change)
self.height_box.setMaximumWidth(100)
self.unit_lbl = QLabel("px")
self.unit_lbl.setMaximumWidth(50)
self.ratio_check = QCheckBox('aspect ratio', self)
self.ratio_check.stateChanged.connect(self.on_ratio_change)
self.apply_btn = QPushButton("Apply")
self.apply_btn.setFixedWidth(90)
self.apply_btn.clicked.connect(self.on_apply)
width_layout = QHBoxLayout()
width_layout.addWidget(self.width_box)
width_layout.addWidget(self.height_box)
width_layout.addWidget(self.unit_lbl)
apply_layout = QHBoxLayout()
apply_layout.addWidget(self.apply_btn)
apply_layout.setAlignment(Qt.AlignRight)
lbl_layout = QHBoxLayout()
lbl_layout.setAlignment(Qt.AlignLeft)
lbl_layout.addWidget(self.width_lbl)
lbl_layout.addWidget(self.height_lbl)
main_layout = QVBoxLayout()
main_layout.setAlignment(Qt.AlignCenter)
main_layout.addLayout(lbl_layout)
main_layout.addLayout(width_layout)
main_layout.addWidget(self.ratio_check)
main_layout.addLayout(apply_layout)
self.setLayout(main_layout)
def set_boxes(self):
self.width_box.setText(str(_img_original.width))
self.height_box.setText(str(_img_original.height))
def on_width_change(self, e):
logger.debug(f"type width {self.width_box.text()}")
if self.ratio_check.isChecked():
r_height = _get_ratio_height(_img_original.width, _img_original.height, int(self.width_box.text()))
self.height_box.setText(str(r_height))
def on_height_change(self, e):
logger.debug(f"type height {self.height_box.text()}")
if self.ratio_check.isChecked():
r_width = _get_ratio_width(_img_original.width, _img_original.height, int(self.height_box.text()))
self.width_box.setText(str(r_width))
def on_ratio_change(self, e):
logger.debug("ratio change")
def on_apply(self, e):
logger.debug("apply")
operations.size = int(self.width_box.text()), int(self.height_box.text())
self.parent.parent.update_img_size_lbl()
self.parent.parent.place_preview_img()
class CommandTab(QWidget):
"""Command tab widget"""
def __init__(self, parent):
super().__init__()
self.parent = parent
self.ipdsl = IPDSL()
self.v = None
self.layout = QtWidgets.QVBoxLayout(self)
# self.button2 = QtWidgets.QPushButton('Start New Session')
self.font = QFont()
self.font.setPointSize(12)
self.chatlog = QtWidgets.QTextEdit()
self.userinput = QtWidgets.QLineEdit()
self.userinput.returnPressed.connect(self.AddToChatLogUser)
# self.button2.clicked.connect(self.getBot)
self.GuiSetup()
def GuiSetup(self):
'''
Styling and Layout.
'''
# alignment to PyQt Widgets
setStyleQte = """QTextEdit {
font-family: "Courier";
font-size: 12pt;
font-weight: 600;
text-align: right;
background-color: Gainsboro;
}"""
setStyletui = """QLineEdit {
font-family: "Courier";
font-weight: 600;
text-align: left;
background-color: White;
}"""
self.chatlog.setStyleSheet(setStyleQte)
self.userinput.setStyleSheet(setStyletui)
self.userinput.setFont(self.font)
# self.button2.setFont(self.font)
# self.layout.addWidget(self.button2)
self.layout.addWidget(self.chatlog)
self.layout.addWidget(self.userinput)
def UpdateCycle(self):
'''
Retrieves a new bot message and appends to the chat log.
'''
bmsg = self.v.getBotMessage()
self.chatlog.setAlignment(Qt.AlignRight)
[self.chatlog.append(m) for m in bmsg]
self.userinput.setFocus()
def AddToChatLogUser(self):
'''
Takes guest's entry and appends to the chatlog
'''
global _img_preview, original_img_path, _img_original
# if _img_preview is None:
# _img_preview = _img_original
umsg = self.userinput.text()
self.chatlog.setAlignment(Qt.AlignLeft)
self.chatlog.append(umsg)
# self.chatlog.setAlignment(Qt.AlignRight)
self.userinput.setText("")
self.ipdsl.original_img = _img_original
try:
_img_preview = self.ipdsl.process(_img_preview, umsg, original_img_path)
if self.ipdsl.msg != '':
self.chatlog.append(self.ipdsl.msg)
self.parent.parent.place_preview_img()
except Exception as e:
self.chatlog.append(" *** Error :" + str(e))
pass
class AdjustingTab(QWidget):
"""Adjusting tab widget"""
def __init__(self, parent):
super().__init__()
self.parent = parent
contrast_lbl = QLabel("Contrast")
contrast_lbl.setAlignment(Qt.AlignCenter)
brightness_lbl = QLabel("Brightness")
brightness_lbl.setAlignment(Qt.AlignCenter)
sharpness_lbl = QLabel("Sharpness")
sharpness_lbl.setAlignment(Qt.AlignCenter)
self.contrast_slider = QSlider(Qt.Horizontal, self)
self.contrast_slider.setMinimum(SLIDER_MIN_VAL)
self.contrast_slider.setMaximum(SLIDER_MAX_VAL)
self.contrast_slider.sliderReleased.connect(self.on_contrast_slider_released)
self.contrast_slider.setToolTip(str(SLIDER_MAX_VAL))
self.brightness_slider = QSlider(Qt.Horizontal, self)
self.brightness_slider.setMinimum(SLIDER_MIN_VAL)
self.brightness_slider.setMaximum(SLIDER_MAX_VAL)
self.brightness_slider.sliderReleased.connect(self.on_brightness_slider_released)
self.brightness_slider.setToolTip(str(SLIDER_MAX_VAL))
self.sharpness_slider = QSlider(Qt.Horizontal, self)
self.sharpness_slider.setMinimum(SLIDER_MIN_VAL)
self.sharpness_slider.setMaximum(SLIDER_MAX_VAL)
self.sharpness_slider.sliderReleased.connect(self.on_sharpness_slider_released)
self.sharpness_slider.setToolTip(str(SLIDER_MAX_VAL))
main_layout = QVBoxLayout()
main_layout.setAlignment(Qt.AlignCenter)
main_layout.addWidget(contrast_lbl)
main_layout.addWidget(self.contrast_slider)
main_layout.addWidget(brightness_lbl)
main_layout.addWidget(self.brightness_slider)
main_layout.addWidget(sharpness_lbl)
main_layout.addWidget(self.sharpness_slider)
self.reset_sliders()
self.setLayout(main_layout)
def reset_sliders(self):
self.brightness_slider.setValue(SLIDER_DEF_VAL)
self.sharpness_slider.setValue(SLIDER_DEF_VAL)
self.contrast_slider.setValue(SLIDER_DEF_VAL)
def on_brightness_slider_released(self):
logger.debug(f"brightness selected value: {self.brightness_slider.value()}")
self.brightness_slider.setToolTip(str(self.brightness_slider.value()))
factor = _get_converted_point(SLIDER_MIN_VAL, SLIDER_MAX_VAL, img_helper.BRIGHTNESS_FACTOR_MIN,
img_helper.BRIGHTNESS_FACTOR_MAX, self.brightness_slider.value())
logger.debug(f"brightness factor: {factor}")
operations.brightness = factor
self.parent.parent.place_preview_img()
def on_sharpness_slider_released(self):
logger.debug(self.sharpness_slider.value())
self.sharpness_slider.setToolTip(str(self.sharpness_slider.value()))
factor = _get_converted_point(SLIDER_MIN_VAL, SLIDER_MAX_VAL, img_helper.SHARPNESS_FACTOR_MIN,
img_helper.SHARPNESS_FACTOR_MAX, self.sharpness_slider.value())
logger.debug(f"sharpness factor: {factor}")
operations.sharpness = factor
self.parent.parent.place_preview_img()
def on_contrast_slider_released(self):
logger.debug(self.contrast_slider.value())
self.contrast_slider.setToolTip(str(self.contrast_slider.value()))
factor = _get_converted_point(SLIDER_MIN_VAL, SLIDER_MAX_VAL, img_helper.CONTRAST_FACTOR_MIN,
img_helper.CONTRAST_FACTOR_MAX, self.contrast_slider.value())
logger.debug(f"contrast factor: {factor}")
operations.contrast = factor
self.parent.parent.place_preview_img()
class FiltersTab(QWidget):
"""Color filters widget"""
def __init__(self, parent):
super().__init__()
self.parent = parent
self.main_layout = QHBoxLayout()
self.main_layout.setAlignment(Qt.AlignCenter)
self.add_filter_thumb("none")
for key, val in color_filter.ColorFilters.filters.items():
self.add_filter_thumb(key, val)
self.setLayout(self.main_layout)
def add_filter_thumb(self, name, title=""):
logger.debug(f"create lbl thumb for: {name}")
thumb_lbl = QLabel()
thumb_lbl.name = name
thumb_lbl.setStyleSheet("border:2px solid #ccc;")
if name != "none":
thumb_lbl.setToolTip(f"Apply <b>{title}</b> filter")
else:
thumb_lbl.setToolTip('No filter')
thumb_lbl.setCursor(Qt.PointingHandCursor)
thumb_lbl.setFixedSize(THUMB_SIZE, THUMB_SIZE)
thumb_lbl.mousePressEvent = partial(self.on_filter_select, name)
self.main_layout.addWidget(thumb_lbl)
def on_filter_select(self, filter_name, e):
logger.debug(f"apply color filter: {filter_name}")
global _img_preview
if filter_name != "none":
_img_preview = img_helper.color_filter(_img_original, filter_name)
else:
_img_preview = _img_original.copy()
operations.color_filter = filter_name
self.toggle_thumbs()
self.parent.parent.place_preview_img()
def toggle_thumbs(self):
for thumb in self.findChildren(QLabel):
color = THUMB_BORDER_COLOR_ACTIVE if thumb.name == operations.color_filter else THUMB_BORDER_COLOR
thumb.setStyleSheet(f"border:2px solid {color};")
class MainLayout(QVBoxLayout):
"""Main layout"""
def __init__(self, parent):
super().__init__()
self.parent = parent
self.img_lbl = QLabel("Press <b>'Upload'</b> to to start<br>"
"<div style='margin: 30px 0'><img src='logo.png' /></div>"
"<b>made with</b> <span style='color:red'>❤</span>")
self.img_lbl.setAlignment(Qt.AlignCenter)
self.file_name = None
self.img_size_lbl = None
self.img_size_lbl = QLabel()
self.img_size_lbl.setAlignment(Qt.AlignCenter)
upload_btn = QPushButton("Upload")
upload_btn.setMinimumWidth(BTN_MIN_WIDTH)
upload_btn.clicked.connect(self.on_upload)
upload_btn.setStyleSheet("font-weight:bold;")
self.reset_btn = QPushButton("Reset")
self.reset_btn.setMinimumWidth(BTN_MIN_WIDTH)
self.reset_btn.clicked.connect(self.on_reset)
# self.reset_btn.setEnabled(False)
self.reset_btn.setStyleSheet("font-weight:bold;")
self.save_btn = QPushButton("Save")
self.save_btn.setMinimumWidth(BTN_MIN_WIDTH)
self.save_btn.clicked.connect(self.on_save)
# self.save_btn.setEnabled(False)
self.save_btn.setStyleSheet("font-weight:bold;")
self.addWidget(self.img_lbl)
self.addWidget(self.img_size_lbl)
self.addStretch()
self.action_tabs = ActionTabs(self)
self.addWidget(self.action_tabs)
# self.action_tabs.setVisible(False)
btn_layout = QHBoxLayout()
btn_layout.setAlignment(Qt.AlignCenter)
btn_layout.addWidget(upload_btn)
btn_layout.addWidget(self.reset_btn)
btn_layout.addWidget(self.save_btn)
self.addLayout(btn_layout)
self.load_default_img()
def place_preview_img(self):
img = _get_img_with_all_operations()
preview_pix = ImageQt.toqpixmap(img)
self.img_lbl.setPixmap(preview_pix)
# QApplication.processEvents()
def on_save(self):
logger.debug("open save dialog")
new_img_path, _ = QFileDialog.getSaveFileName(self.parent, "QFileDialog.getSaveFileName()",
f"ez_pz_{self.file_name}",
"Images (*.png *.jpg)")
if new_img_path:
logger.debug(f"save output image to {new_img_path}")
img = _get_img_with_all_operations()
img.save(new_img_path)
def load_default_img(self):
global original_img_path
img_path = 'traffic_sign.jpg'
original_img_path = img_path
logger.debug(f"open file {img_path}")
self.file_name = ntpath.basename(img_path)
pix = QPixmap(img_path)
self.img_lbl.setPixmap(pix)
self.img_lbl.setScaledContents(True)
self.action_tabs.setVisible(True)
self.action_tabs.adjustment_tab.reset_sliders()
global _img_original
_img_original = ImageQt.fromqpixmap(pix)
self.update_img_size_lbl()
if _img_original.width < _img_original.height:
w = THUMB_SIZE
h = _get_ratio_height(_img_original.width, _img_original.height, w)
else:
h = THUMB_SIZE
w = _get_ratio_width(_img_original.width, _img_original.height, h)
img_filter_thumb = img_helper.resize(_img_original, w, h)
global _img_preview
_img_preview = _img_original.copy()
for thumb in self.action_tabs.filters_tab.findChildren(QLabel):
if thumb.name != "none":
img_filter_preview = img_helper.color_filter(img_filter_thumb, thumb.name)
else:
img_filter_preview = img_filter_thumb
preview_pix = ImageQt.toqpixmap(img_filter_preview)
thumb.setPixmap(preview_pix)
self.reset_btn.setEnabled(True)
self.save_btn.setEnabled(True)
self.action_tabs.modification_tab.set_boxes()
def on_upload(self):
logger.debug("upload")
global original_img_path
img_path, _ = QFileDialog.getOpenFileName(self.parent, "Open image",
"~/Pictures",
"Images (*.png *jpg)")
original_img_path = img_path
if img_path:
logger.debug(f"open file {img_path}")
self.file_name = ntpath.basename(img_path)
pix = QPixmap(img_path)
self.img_lbl.setPixmap(pix)
self.img_lbl.setScaledContents(True)
self.action_tabs.setVisible(True)
self.action_tabs.adjustment_tab.reset_sliders()
global _img_original
_img_original = ImageQt.fromqpixmap(pix)
self.update_img_size_lbl()
if _img_original.width < _img_original.height:
w = THUMB_SIZE
h = _get_ratio_height(_img_original.width, _img_original.height, w)
else:
h = THUMB_SIZE
w = _get_ratio_width(_img_original.width, _img_original.height, h)
img_filter_thumb = img_helper.resize(_img_original, w, h)
global _img_preview
_img_preview = _img_original.copy()
for thumb in self.action_tabs.filters_tab.findChildren(QLabel):
if thumb.name != "none":
img_filter_preview = img_helper.color_filter(img_filter_thumb, thumb.name)
else:
img_filter_preview = img_filter_thumb
preview_pix = ImageQt.toqpixmap(img_filter_preview)
thumb.setPixmap(preview_pix)
self.reset_btn.setEnabled(True)
self.save_btn.setEnabled(True)
self.action_tabs.modification_tab.set_boxes()
def update_img_size_lbl(self):
logger.debug("update img size lbl")
self.img_size_lbl.setText(f"<span style='font-size:11px'>"
f"image size {operations.size[0] if operations.size else _img_original.width} × "
f"{operations.size[1] if operations.size else _img_original.height}"
f"</span>")
def on_reset(self):
logger.debug("reset all")
global _img_preview
_img_preview = _img_original.copy()
operations.reset()
self.action_tabs.filters_tab.toggle_thumbs()
self.place_preview_img()
self.action_tabs.adjustment_tab.reset_sliders()
self.action_tabs.modification_tab.set_boxes()
self.update_img_size_lbl()
class EasyPzUI(QWidget):
"""Main widget"""
def __init__(self):
super().__init__()
self.main_layout = MainLayout(self)
self.setLayout(self.main_layout)
self.setMinimumSize(600, 500)
self.setMaximumSize(900, 900)
self.setGeometry(600, 600, 600, 600)
self.setWindowTitle('Prototype')
self.center()
self.show()
def center(self):
"""align window center"""
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
def closeEvent(self, event):
logger.debug("close")
if operations.has_changes():
reply = QMessageBox.question(self, "",
"You have unsaved changes<br>Are you sure?", QMessageBox.Yes |
QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.Yes:
event.accept()
else:
event.ignore()
def resizeEvent(self, e):
pass
class QVLine(QFrame):
"""Vertical line"""
def __init__(self):
super(QVLine, self).__init__()
self.setFrameShape(QFrame.VLine)
self.setFrameShadow(QFrame.Sunken)
if __name__ == '__main__':
fileConfig('logging_config.ini')
app = QApplication(sys.argv)
win = EasyPzUI()
sys.exit(app.exec_())