Guest User

Untitled

a guest
Jul 18th, 2023
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.08 KB | None | 0 0
  1. import os
  2. import sys
  3. import random
  4. import shutil
  5. from PIL import Image
  6. from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout, QHBoxLayout, QMessageBox
  7. from PyQt5.QtGui import QPixmap, QImage, QPainter, QPen
  8. from PyQt5.QtCore import Qt
  9. from PyQt5.QtGui import QColor
  10.  
  11.  
  12. class ImageComparison(QWidget):
  13.     def __init__(self, image_folder, discarded_folder):
  14.         super().__init__()
  15.         self.image_folder = image_folder
  16.         self.discarded_folder = discarded_folder
  17.         self.image1_path = None
  18.         self.image2_path = None
  19.         self.image_labels = [QLabel(self) for _ in range(2)]
  20.         for label in self.image_labels:
  21.             label.setAlignment(Qt.AlignCenter)
  22.  
  23.         layout = QHBoxLayout()
  24.         for label in self.image_labels:
  25.             layout.addWidget(label, 1)
  26.  
  27.         self.setLayout(layout)
  28.         self.remaining_images = None
  29.  
  30.         self.setWindowTitle('Image Comparison')
  31.         screen_geometry = QApplication.desktop().screenGeometry()
  32.         self.setGeometry(0, 0, screen_geometry.width(), screen_geometry.height())
  33.  
  34.         self.update_images()
  35.  
  36.     def paintEvent(self, event):
  37.         painter = QPainter(self)
  38.         painter.setPen(QPen(Qt.black, 2, Qt.SolidLine))
  39.         center_x = self.width() // 2
  40.         painter.drawLine(center_x, 0, center_x, self.height())
  41.  
  42.     def update_images(self):
  43.         image_files = [f for f in os.listdir(self.image_folder)
  44.                        if os.path.isfile(os.path.join(self.image_folder, f)) and f.lower().endswith('.png')]
  45.  
  46.         if len(image_files) == 2:  # Display remaining images and show popup
  47.             self.remaining_images = image_files
  48.             self.update_remaining_images()
  49.             self.display_remaining_images_popup()
  50.             return
  51.  
  52.         if len(image_files) < 2:  # Stop when there are fewer than two images remaining
  53.             print("Finished!")
  54.             self.close()
  55.             return
  56.  
  57.         self.image1_path, self.image2_path = self.get_random_images(image_files)
  58.  
  59.         self.update_image(self.image1_path, self.image_labels[0])
  60.         self.update_image(self.image2_path, self.image_labels[1])
  61.  
  62.     def update_image(self, image_path, label):
  63.         image = Image.open(os.path.join(self.image_folder, image_path)).convert("RGB")
  64.  
  65.         available_height = self.height() - 40  # Adjust the available height by subtracting the padding (40 pixels)
  66.  
  67.         # Calculate the maximum height for the image
  68.         max_height = available_height - 4  # Subtract 4 pixels for the dividing line and additional padding
  69.  
  70.         # Calculate the maximum width for the images
  71.         max_width = (self.width() - 40) // 2  # Half of the window's width minus a 40-pixel safe space
  72.  
  73.         # Scale the image while maintaining aspect ratio
  74.         image_width, image_height = image.size
  75.  
  76.         scale_factor_height = max_height / image_height
  77.         scale_factor_width = max_width / image_width
  78.  
  79.         scale_factor = min(scale_factor_height, scale_factor_width)
  80.         new_width = int(image_width * scale_factor)
  81.         new_height = int(image_height * scale_factor)
  82.         image = image.resize((new_width, new_height), Image.Resampling.LANCZOS)
  83.  
  84.         qimage = self.convert_pil_to_qimage(image)
  85.         pixmap = QPixmap.fromImage(qimage)
  86.  
  87.         # Adjust the label size to avoid overlapping with the divider line
  88.         label.setMaximumSize(max_width, available_height)
  89.  
  90.         label.setPixmap(pixmap)
  91.  
  92.  
  93.     def convert_pil_to_qimage(self, pil_image):
  94.         data = pil_image.convert("RGBA").tobytes("raw", "RGBA")
  95.         qimage = QImage(data, pil_image.size[0], pil_image.size[1], QImage.Format_RGBA8888)
  96.         return qimage
  97.  
  98.     def keyPressEvent(self, event):
  99.         key_mapping = {
  100.             Qt.Key_Left: self.image1_path,
  101.             Qt.Key_A: self.image1_path,
  102.             Qt.Key_Right: self.image2_path,
  103.             Qt.Key_D: self.image2_path,
  104.             Qt.Key_Down: (self.image1_path, self.image2_path),
  105.             Qt.Key_S: (self.image1_path, self.image2_path),
  106.             Qt.Key_Up: None,
  107.             Qt.Key_W: None,
  108.         }
  109.  
  110.         key = event.key()
  111.         image_paths = key_mapping.get(key)
  112.  
  113.         if image_paths:
  114.             if isinstance(image_paths, tuple):
  115.                 for image_path in image_paths:
  116.                     self.move_to_discarded(image_path)
  117.             else:
  118.                 self.move_to_discarded(image_paths)
  119.         elif key in (Qt.Key_Up, Qt.Key_W):
  120.             pass  # Do nothing, no image will be moved
  121.  
  122.         self.update_images()
  123.  
  124.     def move_to_discarded(self, image_path):
  125.         os.makedirs(self.discarded_folder, exist_ok=True)
  126.         shutil.move(os.path.join(self.image_folder, image_path), self.discarded_folder)
  127.         print(f"{image_path} discarded")
  128.  
  129.     def get_random_images(self, image_files):
  130.         image1_path = random.choice(image_files)
  131.         image_files.remove(image1_path)
  132.         image2_path = random.choice(image_files)
  133.         return image1_path, image2_path
  134.  
  135.     def update_remaining_images(self):
  136.         for image_path, label in zip(self.remaining_images, self.image_labels):
  137.             self.update_image(image_path, label)
  138.  
  139.     def display_remaining_images_popup(self):
  140.         # Show the remaining images in the main window
  141.         self.update_remaining_images()
  142.  
  143.         # Display a message box with the remaining image paths
  144.         msg_box = QMessageBox()
  145.         msg_box.setWindowTitle("Image Comparison")
  146.         msg_box.setText("2 images remaining")
  147.         msg_box.setInformativeText("Remaining images:\n\n" + "\n".join(self.remaining_images))
  148.  
  149.         # Show the message box
  150.         msg_box.exec_()
  151.  
  152.  
  153. def main():
  154.     app = QApplication(sys.argv)
  155.  
  156.     if len(sys.argv) > 1:
  157.         folder_path = sys.argv[1]
  158.     else:
  159.         folder_path = input("Enter the folder path: ")
  160.  
  161.     discarded_dir = os.path.join(folder_path, "discarded")
  162.     image_comparison = ImageComparison(folder_path, discarded_dir)
  163.     image_comparison.showMaximized()
  164.  
  165.     sys.exit(app.exec_())
  166.  
  167.  
  168. if __name__ == '__main__':
  169.     main()
  170.  
Advertisement
Add Comment
Please, Sign In to add comment