import sys
import copy

from PySide6.QtWidgets import QApplication, QMainWindow, QGraphicsView, QGraphicsScene, QGraphicsPixmapItem
from PySide6.QtGui import QPixmap, QImage, QPainter
from PySide6.QtCore import QRectF


# Test images
IMAGE_A = "test_image_a.jpg"
IMAGE_B = "test_image_b.jpg"


class DifferenceViewer(QMainWindow):
    def __init__(self):
        super().__init__()

        self.image_a = QImage(IMAGE_A)
        self.image_b = QImage(IMAGE_B)
        self.composite_image = None

        self.scene = QGraphicsScene()
        self.view = QGraphicsView()
        self.view.setScene(self.scene)
        self.view.setDragMode(QGraphicsView.ScrollHandDrag)
        self.view.setRenderHint(QPainter.SmoothPixmapTransform)
        self.view.setRenderHint(QPainter.Antialiasing)
        self.view.setViewportUpdateMode(QGraphicsView.FullViewportUpdate)

        self.setMinimumSize(800, 600)
        self.setCentralWidget(self.view)

        self.show_differences()


    def show_differences(self):
        """
        Inverts `self.image_b` and blends it with 50% opacity over
        `self.image_a` making equal color pixels appear gray.
        """
        composite = copy.copy(self.image_a)

        other = copy.copy(self.image_b)
        other.invertPixels()

        painter = QPainter()
        painter.begin(composite)
        painter.setOpacity(0.5)
        painter.drawImage(0, 0, other)
        painter.end()

        self.composite_image = composite
        self.display_image(composite)


    def display_image(self, image: QImage):
        """
        This method cleans `sef.scene` and displays a new image. It's not
        really necessary in this example, but I decided to include it anyway...
        """
        self.scene.clear()
        self.view.update()
        pixmap = QPixmap.fromImage(image)
        item = QGraphicsPixmapItem(pixmap)
        self.scene.addItem(item)
        self.view.setSceneRect(QRectF(pixmap.rect()))
        self.view.setScene(self.scene)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    viewer = DifferenceViewer()
    viewer.show()
    sys.exit(app.exec())
