-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgui.py
660 lines (518 loc) · 25.9 KB
/
gui.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
657
658
659
660
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QFrame, QLabel, QFileDialog, QSlider,QInputDialog
from PyQt6.QtGui import QIcon, QPixmap, QFont, QImage, QMouseEvent, QKeyEvent
from PyQt6.QtCore import Qt, QSize, QPoint
import sys, os
import numpy as np
import cv2
MAIN_ICON = "./assets/icons/main_icon.png"
BG_COLOR = "#121212"
CANVAS_COLOR = "#212121"
SPANEL_COLOR = "#282828"
SPANEL_TXT_COLOR = "#787878"
SPANEL_HEADING_COLOR = "#3d3b3b"
class Window(QWidget):
def __init__(self):
super().__init__()
self.zoom_factor = 1.0
# Get screen size
screen = QApplication.primaryScreen()
screen_geometry = screen.geometry()
self.setGeometry(screen_geometry)
# Window properties
self.setWindowTitle("Edify-X")
self.setWindowIcon(QIcon(MAIN_ICON))
self.setStyleSheet(f"background-color: {BG_COLOR};") # Apply main background color
# Main layout (horizontal)
main_layout = QHBoxLayout()
self.setLayout(main_layout)
# Create and add toolbar
toolbar_widget = self.create_toolbar()
main_layout.addWidget(toolbar_widget)
# Create and add canvas at the center
self.canvas = self.create_canvas()
main_layout.addWidget(self.canvas, 1)
# Create and add side panel at the right side
side_panel = self.create_side_panel()
main_layout.addWidget(side_panel)
# Push other elements to the right
main_layout.addStretch() # Push other elements to the right
self.image_selected = False
self.dragging = False
self.image_pos = QPoint(0,0)
self.last_mouse_pos = QPoint(0,0)
# ------- Left Toolbar ----------- #
def create_toolbar(self):
toolbar_widget = QWidget() # Wrapper for styling
toolbar_widget.setFixedWidth(45) # Set toolbar width
toolbar_widget.setStyleSheet(f"background-color: {CANVAS_COLOR};") # Apply background color
toolbar_layout = QVBoxLayout()
toolbar_layout.setSpacing(30)
toolbar_layout.setAlignment(Qt.AlignmentFlag.AlignCenter)
icons = {
"Import": "Import.svg",
"Export": "Export.svg",
"Brush": "Brush.svg",
"Select": "Select.svg",
"Rotate": "Rotate.svg",
"Crop": "Crop.svg",
"Zoom-in": "Zoom-in.svg",
"Zoom-out": "Zoom-out.svg",
"Trash": "Trash.svg"
}
for name, icon in icons.items():
button = QPushButton()
button.setIcon(QIcon(f"./assets/icons/{icon}")) # Set SVG as icon
button.setIconSize(QSize(15, 15)) # Adjust icon size
button.setStyleSheet("""
QPushButton {
background-color: transparent;
color: white;
padding: 5px;
border: none;
}
QPushButton:hover {
background-color: rgba(255, 255, 255, 0.1); /* Light transparent white */
border-radius: 5px;
}
""")
if name == "Import":
button.clicked.connect(self.choose_image)
elif name == "Export":
button.clicked.connect(self.export_image)
elif name == "Select":
button.clicked.connect(self.enable_selection)
elif name == "Rotate":
button.clicked.connect(self.rotate_image)
elif name == "Crop":
button.clicked.connect(self.start_crop)
elif name == "Zoom-in":
button.clicked.connect(self.zoom_in)
elif name == "Zoom-out":
button.clicked.connect(self.zoom_out)
elif name == "Trash":
button.clicked.connect(self.clear_canvas)
elif name == "Export":
button.clicked.connect(self.export_image)
elif name == "Crop":
button.clicked.connect(self.start_crop)
elif name == "Brush":
button.clicked.connect(self.enable_brush)
toolbar_layout.addWidget(button)
toolbar_widget.setLayout(toolbar_layout) # Set layout inside widget
return toolbar_widget # Return wrapped toolbar
#------- Center Canas ----------#
def create_canvas(self):
canvas_wrapper = QWidget()
canvas_layout = QVBoxLayout(canvas_wrapper)
canvas_layout.setContentsMargins(30, 30, 30, 30)
canvas = QFrame()
canvas.setStyleSheet(f"background-color: {CANVAS_COLOR}; border: 2px solid {SPANEL_COLOR};")
canvas.setMinimumSize(900, 500)
self.image_label = QLabel(canvas)
self.image_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.image_label.setGeometry(0, 0, 900, 500)
self.image_label.setScaledContents(False)
canvas_layout.addWidget(canvas, alignment = Qt.AlignmentFlag.AlignCenter)
return canvas_wrapper
# ----------------- Side Panel ------------------------
def create_side_panel(self):
side_panel_frame = QFrame()
side_panel_frame.setStyleSheet(f"background-color: {CANVAS_COLOR}; border-radius: 8px;")
side_panel_frame.setFixedWidth(220)
side_layout = QVBoxLayout(side_panel_frame)
side_layout.setSpacing(10)
side_layout.setContentsMargins(10, 10, 10, 10)
# -------- Blending Section --------
blending_layout = QVBoxLayout()
blending_label = QLabel("Blending")
blending_label.setFont(QFont("Arial", 12, QFont.Weight.Bold))
blending_label.setStyleSheet(f"color: {SPANEL_HEADING_COLOR};")
blending_label.setAlignment(Qt.AlignmentFlag.AlignTop | Qt.AlignmentFlag.AlignHCenter)
blending_layout.addWidget(blending_label)
self.saturation_slider = QSlider(Qt.Orientation.Horizontal)
self.saturation_slider.setRange(-100, 100) # Left = B/W, Right = Max Saturation
self.saturation_slider.setValue(0) # Default = Normal saturation
self.saturation_slider.setVisible(False) # Initially hidden
self.saturation_slider.valueChanged.connect(self.update_saturation)
# Add stretch above blending box to push it towards center
blending_layout.addStretch(1)
blending_box = QFrame()
blending_box.setStyleSheet(f"background-color: {SPANEL_COLOR}; border-radius: 8px; padding: 10px;")
blending_box_layout = QVBoxLayout(blending_box)
blending_options = ["Hue", "Saturation", "Luminosity"]
for option in blending_options:
btn = QPushButton(option)
btn.setStyleSheet("""
QPushButton {
background-color: transparent;
color: #787878;
padding: 5px;
border: none;
}
QPushButton:hover {
background-color: rgba(255, 255, 255, 0.1);
border-radius: 5px;
}
""")
btn.clicked.connect(lambda checked, opt=option: self.adjust_blending(opt)) # Connect to adjust_blending method
blending_box_layout.addWidget(btn)
# Add a slider below each button
if option == "Hue":
self.hue_slider = QSlider(Qt.Orientation.Horizontal)
self.hue_slider.setRange(0, 360) # Hue ranges from 0 to 360 degrees
self.hue_slider.setValue(0) # Default value
self.hue_slider.setVisible(False) # Initially hidden
self.hue_slider.valueChanged.connect(self.update_hue) # Connect to update_hue method
blending_box_layout.addWidget(self.hue_slider)
elif option == "Saturation":
self.saturation_slider = QSlider(Qt.Orientation.Horizontal)
self.saturation_slider.setRange(0, 200) # Saturation ranges from 0% to 200%
self.saturation_slider.setValue(100) # Default value (100% saturation)
self.saturation_slider.setVisible(False) # Initially hidden
self.saturation_slider.valueChanged.connect(self.update_saturation) # Connect to update_saturation method
blending_box_layout.addWidget(self.saturation_slider)
elif option == "Luminosity":
self.luminosity_slider = QSlider(Qt.Orientation.Horizontal)
self.luminosity_slider.setRange(0, 200) # Luminosity ranges from 0% to 200%
self.luminosity_slider.setValue(100) # Default value (100% luminosity)
self.luminosity_slider.setVisible(False) # Initially hidden
self.luminosity_slider.valueChanged.connect(self.update_luminosity) # Connect to update_luminosity method
blending_box_layout.addWidget(self.luminosity_slider)
blending_layout.addWidget(blending_box)
# Add stretch below the blending box to push it towards center
blending_layout.addStretch(2)
# -------- Adding to Side Panel Layout --------
side_layout.addLayout(blending_layout)
return side_panel_frame
def choose_image(self):
#open a file dialogue for image selection
file_dialogue = QFileDialog()
file_path, _ = file_dialogue.getOpenFileName(self, "Select Image", "", "Images(*.png *.jpg *.jpeg )")
if file_path:
print(f"Selected image: {file_path}")
self.display_image(file_path)
def display_image(self, image_path):
self.cv_image = cv2.imread(image_path)
if self.cv_image is not None:
self.image_path = image_path
self.original_image = self.cv_image.copy()
self.update_image_display()
# Enable selection by clicking on the image
self.image_label.mousePressEvent = self.select_image
self.image_label.mouseMoveEvent = self.move_image
self.image_label.mouseReleaseEvent = self.stop_moving
# Enable deselection when clicking outside the image
self.canvas.mousePressEvent = self.deselect_image
def update_image_display(self, image = None):
"""Updates QLabel with the image"""
if self.cv_image is None:
return
# Resize the image based on the zoom factor
height, width = self.cv_image.shape[:2]
new_width = int(width * self.zoom_factor)
new_height = int(height * self.zoom_factor)
resized_image = cv2.resize(self.cv_image, (new_width, new_height))
rgb_image = cv2.cvtColor(resized_image, cv2.COLOR_BGR2RGB)
h, w, ch = rgb_image.shape
bytes_per_line = ch * w
qimage = QImage(rgb_image.data, w, h, bytes_per_line, QImage.Format.Format_RGB888)
pixmap = QPixmap.fromImage(qimage)
self.image_label.setPixmap(pixmap)
self.image_label.resize(w, h)
self.image_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
if self.image_selected:
self.image_label.setStyleSheet("border: 3px solid #6B679C;") # Green border
else:
self.image_label.setStyleSheet("border: none;")
def enable_brush(self):
"""Activates brush mode and asks user for brush settings"""
print("Brush Mode Enabled")
# Disable other tools
self.is_cropping = False
self.is_moving = False
self.is_drawing = False # Ensure brush is reset
# Ask for brush color
color, ok = QInputDialog.getText(self, "Brush Color", "Enter Hex Color Code (#RRGGBB):")
if not ok or not color.startswith("#") or len(color) != 7:
print("Invalid color! Defaulting to blue.")
color = "#0000FF" # Default to blue if input is invalid
# Ask for brush size
size, ok = QInputDialog.getInt(self, "Brush Size", "Enter Brush Size:", min=1, max=50)
if not ok:
print("Invalid size! Defaulting to 3px.")
size = 3 # Default size if input is invalid
# Convert hex color to BGR (OpenCV format)
self.brush_color = tuple(int(color[i:i+2], 16) for i in (5, 3, 1)) # Convert hex to BGR
self.brush_size = size
# Enable drawing mode
self.is_drawing = True
# Assign drawing events
self.image_label.mousePressEvent = self.start_drawing
self.image_label.mouseMoveEvent = self.draw
self.image_label.mouseReleaseEvent = self.stop_drawing
def start_drawing(self, event):
"""Starts drawing on the image"""
if self.is_drawing and event.button() == Qt.MouseButton.LeftButton:
self.last_point = event.pos()
def draw(self, event):
"""Draws on the image with the brush"""
if self.is_drawing and self.last_point is not None:
if self.cv_image is None:
return
img_height, img_width = self.cv_image.shape[:2]
label_width = self.image_label.width()
label_height = self.image_label.height()
scale_x = img_width / label_width
scale_y = img_height / label_height
x1 = int(self.last_point.x() * scale_x)
y1 = int(self.last_point.y() * scale_y)
x2 = int(event.pos().x() * scale_x)
y2 = int(event.pos().y() * scale_y)
cv2.line(self.cv_image, (x1, y1), (x2, y2), self.brush_color, self.brush_size) # Brush color: Blue, Thickness: 3px
self.last_point = event.pos() # Update last position
self.update_image_display() # Refresh image display
def stop_drawing(self, event):
"""Stops drawing when the mouse is released"""
if self.is_drawing:
self.last_point = None
def enable_selection(self):
"""Activates selection mode and disables other tools"""
print("Selection Mode Enabled")
# Disable brush mode
self.is_drawing = False
self.image_selected = True
self.update_image_display()
# Assign movement-related events to the image label
self.image_label.mousePressEvent = self.select_image
self.image_label.mouseMoveEvent = self.move_image
self.image_label.mouseReleaseEvent = self.stop_moving
def select_image(self, event):
"""Selects the image and enables movement"""
if self.image_selected:
self.dragging = True
self.last_mouse_pos = event.pos() # Store initial mouse position
print("Image Selected for Movement")
def deselect_image(self, event):
"""Deselects the image when clicking outside it"""
print("Image Deselected")
self.image_selected = False
self.dragging = False
self.update_image_display()
def move_image(self, event: QMouseEvent):
"""Moves the image when dragging"""
if self.dragging:
new_pos = event.pos() - self.last_mouse_pos
self.image_pos += new_pos # Update image position
self.image_label.move(self.image_pos) # Move QLabel
self.last_mouse_pos = event.pos()
def stop_moving(self, event):
"""Stops moving the image when mouse is released"""
if self.dragging:
self.dragging = False
print("Stopped Moving Image")
def zoom_in(self):
"""Zoom in on the image"""
if self.cv_image is not None:
self.zoom_factor *= 1.2 # Increase zoom factor
self.update_image_display()
def zoom_out(self):
"""Zoom out on the image"""
if self.cv_image is not None:
self.zoom_factor /= 1.2 # Decrease zoom factor
self.update_image_display()
def clear_canvas(self):
"""Clear the canvas and reset the image label"""
self.cv_image = None # Clear the current image
self.zoom_factor = 1.0 # Reset zoom factor
self.image_label.clear() # Clear the image label
def export_image(self):
"""Open a file dialog to save the modified image"""
if self.cv_image is None:
print("No image to export.")
return # Exit if there is no image to export
# Open a file dialog to choose the save location
file_dialogue = QFileDialog()
file_path, _ = file_dialogue.getSaveFileName(self, "Save Image", "", "Images (*.png *.jpg *.jpeg)")
if file_path:
# Save the current image to the chosen file path
success = cv2.imwrite(file_path, self.cv_image)
if success:
print(f"Image saved successfully at: {file_path}")
else:
print("Error saving the image.")
def start_crop(self):
"""Implementing crop"""
self.is_cropping = True
self.start_point = None
self.end_point = None
self.image_label.setCursor(Qt.CursorShape.CrossCursor)
self.image_label.mousePressEvent = self.mouse_press_event
self.image_label.mouseMoveEvent = self.mouse_move_event
self.image_label.mouseReleaseEvent = self.mouse_release_event
def mouse_press_event(self, event):
"""Get the starting coordinates of the mouse"""
if event.button() == Qt.MouseButton.LeftButton and self.is_cropping:
self.start_point = event.pos()
self.end_point = event.pos()
def mouse_move_event(self, event):
"""Updates the end point of mouse while dragging"""
if self.is_cropping and self.start_point is not None:
self.end_point = event.pos()
self.update_crop_rectangle()
def mouse_release_event(self, event):
"""Finalizez the crop selection"""
if event.button() == Qt.MouseButton.LeftButton and self.is_cropping:
self.is_cropping = False
self.crop_image()
self.image_label.setCursor(Qt.CursorShape.ArrowCursor)
self.image_label.mousePressEvent = None
self.image_label.mouseMoveEvent = None
self.image_label.mouseReleaseEvent = None
def crop_image(self):
"""Crops the selected area from the image"""
if self.start_point and self.end_point:
if not hasattr(self, "original_image") or self.original_image is None:
print("Error: No original image to crop from!")
return
label_width = self.image_label.width()
label_height = self.image_label.height()
img_height, img_width = self.cv_image.shape[:2]
scale_x = img_width / label_width
scale_y = img_height / label_height
x1 = int(round(min(self.start_point.x(), self.end_point.x()) * scale_x))
y1 = int(round(min(self.start_point.y(), self.end_point.y()) * scale_y))
x2 = int(round(max(self.start_point.x(), self.end_point.x()) * scale_x))
y2 = int(round(max(self.start_point.y(), self.end_point.y()) * scale_y))
# Ensure the coordinates are within the image bounds
x1 = max(0, x1)
y1 = max(0, y1)
x2 = min(img_width, x2)
y2 = min(img_height, y2)
# Crop the image
self.cv_image = self.original_image[y1:y2, x1:x2].copy()
self.update_image_display()
self.is_cropping = False
self.start_point = None
self.end_point = None
self.image_label.setCursor(Qt.CursorShape.ArrowCursor)
self.image_label.mousePressEvent = self.select_image
self.image_label.mouseMoveEvent = self.move_image
self.image_label.mouseReleaseEvent = self.stop_moving
def update_crop_rectangle(self):
if self.start_point and self.end_point:
temp_image = self.cv_image.copy()
label_width = self.image_label.width()
label_height = self.image_label.height()
img_height, img_width = self.cv_image.shape[:2]
scale_x = img_width / label_width
scale_y = img_height / label_height
"""Get the coordinates for the rectangle"""
x1 = int(min(self.start_point.x(), self.end_point.x()) * scale_x)
y1 = int(min(self.start_point.y(), self.end_point.y()) * scale_y)
x2 = int(max(self.start_point.x(), self.end_point.x()) * scale_x)
y2 = int(max(self.start_point.y(), self.end_point.y()) * scale_y)
cv2.rectangle(temp_image, (x1, y1), (x2, y2), (0, 255, 255), 2) # White rectangle
self.update_image_display(temp_image)
def adjust_blending(self, option):
"""Show the slider for adjusting the selected blending option"""
if option == "Hue":
self.hue_slider.setVisible(True) # Show the hue slider
self.saturation_slider.setVisible(False) # Hide the saturation slider
self.luminosity_slider.setVisible(False) # Hide the luminosity slider
elif option == "Saturation":
self.saturation_slider.setVisible(True) # Show the saturation slider
self.hue_slider.setVisible(False) # Hide the hue slider
self.luminosity_slider.setVisible(False) # Hide the luminosity slider
elif option == "Luminosity":
self.luminosity_slider.setVisible(True) # Show the luminosity slider
self.hue_slider.setVisible(False) # Hide the hue slider
self.saturation_slider.setVisible(False) # Hide the saturation slider
def update_hue(self):
if self.cv_image is not None:
hue_value = self.hue_slider.value() # Get slider value (0-360)
# OpenCV hue range is 0-179, so we scale it
hue_shift = int((hue_value / 100) * 50)
# Convert image to HSV
hsv_image = cv2.cvtColor(self.cv_image, cv2.COLOR_BGR2HSV)
# Apply hue shift correctly
hsv_image[:, :, 0] = (hsv_image[:, :, 0] + hue_shift) % 180
# Convert back to BGR and update display
self.cv_image = cv2.cvtColor(hsv_image, cv2.COLOR_HSV2BGR)
self.update_image_display() # Update the displayed image
def update_saturation(self):
"""Adjusts the image saturation from black & white (-100) to max saturation (+100)."""
if self.cv_image is not None:
sat_value = self.saturation_slider.value() # Get slider value (-100 to 100)
# Convert to HSV color space
hsv_image = cv2.cvtColor(self.original_image, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv_image) # Split into channels
if sat_value == -100:
# Completely remove saturation by setting S channel to 0
s[:] = 0
else:
if sat_value < 0:
# Reduce saturation (fade to grayscale)
scale = 1 + (sat_value / 100) # Scale factor between 0 (B/W) and 1 (Original)
s = (s * scale).astype(np.uint8)
else:
# Increase saturation (boost colors)
scale = 1 + (sat_value / 100) # Scale factor between 1 (Original) and 2 (Max Boost)
s = np.clip(s * scale, 0, 255).astype(np.uint8) # Keep values within range
# Merge modified channels back and convert to BGR
hsv_image = cv2.merge([h, s, v])
self.cv_image = cv2.cvtColor(hsv_image, cv2.COLOR_HSV2BGR)
# Update displayed image
self.update_image_display()
def update_luminosity(self):
"""Adjusts the image luminosity from dark (-100) to bright (+100)."""
if self.cv_image is not None:
lum_value = self.luminosity_slider.value() # Get slider value (-100 to 100)
# Convert image to HSV
hsv_image = cv2.cvtColor(self.original_image, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv_image) # Split into channels
if lum_value < 0:
# Reduce brightness (darken image)
scale = 1 + (lum_value / 100) # Scale factor between 0 (Dark) and 1 (Original)
v = (v * scale).astype(np.uint8)
else:
# Increase brightness (lighten image)
scale = 1 + (lum_value / 100) # Scale factor between 1 (Original) and 2 (Max Brightness)
v = np.clip(v * scale, 0, 255).astype(np.uint8) # Ensure valid range
# Merge modified channels back and convert to BGR
hsv_image = cv2.merge([h, s, v])
self.cv_image = cv2.cvtColor(hsv_image, cv2.COLOR_HSV2BGR)
# Update displayed image
self.update_image_display()
def rotate_image(self):
"""Rotates the image by a user-defined angle."""
if self.cv_image is not None:
# Create input dialog for rotation angle
dialog = QInputDialog(self)
dialog.setWindowTitle("Rotate Image")
dialog.setLabelText("Enter rotation angle (degrees):")
dialog.setStyleSheet("QWidget { background-color: white; }") # Set text box color to white
angle, ok = dialog.getInt(self, "Rotate Image", "Enter rotation angle (degrees):", 0, -360, 360, 1)
if ok: # Check if the user clicked OK
# Rotate the image
self.cv_image = self.rotate_image_by_angle(self.cv_image, angle)
self.update_image_display()
print(f"Image Rotated by {angle} degrees")
def rotate_image_by_angle(self, image, angle):
"""Rotates the image by the specified angle."""
# Get the image dimensions
(h, w) = image.shape[:2]
# Calculate the center of the image
center = (w // 2, h // 2)
# Create the rotation matrix
M = cv2.getRotationMatrix2D(center, angle, 1.0)
# Perform the rotation
rotated_image = cv2.warpAffine(image, M, (w, h))
return rotated_image
def start():
app = QApplication([])
window = Window()
window.show()
sys.exit(app.exec())
#-------------------------------------------------------------------#
if __name__ == "__main__":
start()