fix image resizing and general code of image class

This commit is contained in:
2026-03-22 01:41:16 +01:00
parent d4b7067484
commit b59768bcff
+44 -13
View File
@@ -1,29 +1,60 @@
from PySide6.QtGui import QImage, QPixmap from PySide6.QtGui import QImage, QPixmap
from PySide6.QtWidgets import QLabel from PySide6.QtWidgets import QLabel
from utils.data.state import state from utils.data.state import state
from PySide6.QtCore import Qt from PySide6.QtCore import Qt, QSize, QEvent, QObject
import os import os
class Image(): class Image(QObject):
TARGET_WIDTH = 300
def __init__(self, parent): def __init__(self, parent):
super().__init__(parent)
self.application = parent
self.overlay_label = QLabel(parent) self.overlay_label = QLabel(parent)
self.overlay_label.setAttribute(Qt.WidgetAttribute.WA_TransparentForMouseEvents, True) self.overlay_label.setAttribute(Qt.WidgetAttribute.WA_TransparentForMouseEvents, True)
self._current_image_path = None
if state.image_path is not None and os.path.exists(state.image_path): parent.installEventFilter(self)
self.image = QImage(state.image_path)
self.image = self.image.scaledToWidth(300, Qt.TransformationMode.SmoothTransformation)
self.pixmap = QPixmap.fromImage(self.image) if state.image_path and os.path.exists(state.image_path):
self.overlay_label.setPixmap(self.pixmap) self._load_and_display(state.image_path)
def eventFilter(self, obj, event):
if obj == self.application and event.type() == QEvent.Type.Resize:
if self._current_image_path:
self._load_and_display(self._current_image_path)
return False
def _load_and_display(self, image_path):
self._current_image_path = image_path
parent = self.application
image = QImage(image_path)
if image.isNull():
self.overlay_label.hide()
return
scaled_image = image.scaledToWidth(
self.TARGET_WIDTH, Qt.TransformationMode.SmoothTransformation
)
max_width = parent.width()
max_height = parent.height()
if scaled_image.width() > max_width or scaled_image.height() > max_height:
scaled_image = scaled_image.scaled(
QSize(max_width, max_height),
Qt.AspectRatioMode.KeepAspectRatio,
Qt.TransformationMode.SmoothTransformation,
)
pixmap = QPixmap.fromImage(scaled_image)
self.overlay_label.setPixmap(pixmap)
self.overlay_label.adjustSize() self.overlay_label.adjustSize()
self.overlay_label.raise_() self.overlay_label.raise_()
x = parent.width() - self.overlay_label.width() x = parent.width() - self.overlay_label.width() - 100
y = parent.height() - self.overlay_label.height() y = parent.height() - self.overlay_label.height() - 100
self.overlay_label.move(x, y) self.overlay_label.move(x, y)
self.overlay_label.show()
def update_image_overlay(self, new_image_path): def update_image_overlay(self, new_image_path):
self.image = QImage(new_image_path) self._load_and_display(new_image_path)
self.pixmap = QPixmap.fromImage(self.image)
self.overlay_label.setPixmap(self.pixmap)
self.overlay_label.adjustSize()