studia/jezyki-skryptowe/image-editor/main.py

133 lines
4.1 KiB
Python

import sys
from PyQt6.QtWidgets import QApplication, QLabel, QVBoxLayout, QHBoxLayout, QPushButton, QWidget, QFileDialog, QSlider, QLineEdit
from PyQt6.QtGui import QPixmap, QImage, QColor, QPainter, QPen
from PyQt6.QtCore import Qt, QPoint, QThread
import cv2
import numpy as np
from ImageCanvas import ImageCanvas
from ImageProcessingWorker import ImageProcessingWorker
from HueDialog import HueDialog
def process_image_function(image, values):
saturation = values.get('saturation', 0.0)
contrast = values.get('contrast', 1.0)
brightness = values.get('brightness', 0)
# Adjust saturation
hsv_image[:, :, 1] += saturation * 255
hsv_image[:, :, 1] = np.clip(hsv_image[:, :, 1], 0, 255)
# Adjust brightness and contrast
hsv_image[:, :, 2] = np.clip(hsv_image[:, :, 2] * contrast + brightness, 0, 255)
return rgb_image
class ImageEditor(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Image Editor")
self.setGeometry(100, 100, 800, 600)
self.canvas = ImageCanvas()
# self.image_label = QLabel()
# self.image_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.open_button = QPushButton("Open Image")
self.open_button.clicked.connect(self.open_image)
self.save_button = QPushButton("Save Image")
self.save_button.clicked.connect(self.save_image)
self.hcl_button = QPushButton("Hue-Chroma-Lightness")
self.hcl_button.clicked.connect(self.open_hcl)
self.resolution_label = QLabel("Resolution:")
self.width_input = QLineEdit()
self.height_input = QLineEdit()
self.aspect_ratio_label = QLabel("Aspect Ratio:")
self.aspect_ratio_input = QLineEdit()
main_layout = QHBoxLayout()
side_panel = QVBoxLayout()
side_panel.addWidget(self.open_button)
side_panel.addWidget(self.save_button)
side_panel.addWidget(self.hcl_button)
preview_panel = QVBoxLayout()
preview_panel.addWidget(self.canvas)
main_layout.addLayout(side_panel,2)
main_layout.addLayout(preview_panel, 3)
self.setLayout(main_layout)
def open_image(self):
file_name, _ = QFileDialog.getOpenFileName(self, "Open Image File", "", "Image Files (*.jpg *.jpeg *.png)")
if file_name:
self.image = cv2.imread(file_name)
self.image = cv2.cvtColor(self.image, cv2.COLOR_BGR2RGB)
self.image_as_hsv = cv2.cvtColor(self.image, cv2.COLOR_RGB2HSV).astype('float32')
self.display_image(self.image, first_load=True)
def display_image(self, image, first_load = False):
height, width, channel = image.shape
bytes_per_line = 3 * width
q_image = QPixmap.fromImage(QImage(image.data, width, height, bytes_per_line, QImage.Format.Format_RGB888))
self.canvas.updatePixmap(QImage(image.data, width, height, bytes_per_line, QImage.Format.Format_RGB888))
if first_load:
self.canvas.reset()
def save_image(self):
file_name, _ = QFileDialog.getSaveFileName(self, "Save Image File", "", "Image Files (*.jpg *.png)")
if file_name and hasattr(self, 'image'):
cv2.imwrite(file_name, cv2.cvtColor(self.image, cv2.COLOR_RGB2BGR))
def open_hcl(self):
self.dialog = HueDialog(self.image_as_hsv)
self.dialog.result_ready().connect(self.display_image)
self.dialog.exec()
def update_image(self):
print(self.saturation_slider.value(), self.contrast_slider.value(), self.brightness_slider.value())
if self.image_as_hsv is not None:
if self.worker is None:
self.setup_worker()
values = {
'saturation': self.saturation_slider.value() / 100.0,
'contrast': self.contrast_slider.value() / 100.0,
'brightness': self.brightness_slider.value()
}
self.update_values_signal.emit(values) # Emit new values to the worker
if __name__ == "__main__":
app = QApplication(sys.argv)
editor = ImageEditor()
editor.show()
sys.exit(app.exec())