shh_algo_PY

Shankra - Editor P2

May 7th, 2022
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.95 KB | None | 0 0
  1. import os
  2.  
  3. from PyQt5.QtWidgets import (
  4.    QApplication, QWidget,
  5.    QFileDialog, # Dialogue for opening files (and folders)
  6.    QLabel, QPushButton, QListWidget,
  7.    QHBoxLayout, QVBoxLayout
  8. )
  9.  
  10. '''Adding libraries'''
  11. from PyQt5.QtCore import Qt
  12. # For Qt.KeepAspectRatio constant to resize while maintaining proportions
  13. from PyQt5.QtGui import QPixmap # screen-optimisation (pixel map)
  14. from PIL import Image # For all the image properties
  15.  
  16. # Make the window
  17. app = QApplication([])
  18. win = QWidget()      
  19. win.resize(700, 500)
  20. win.setWindowTitle('Easy Editor')
  21. lb_image = QLabel("Image")
  22. btn_dir = QPushButton("Folder")
  23. lw_files = QListWidget()
  24.  
  25. # Make all the buttons
  26. btn_left = QPushButton("Left")
  27. btn_right = QPushButton("Right")
  28. btn_flip = QPushButton("Mirror")
  29. btn_sharp = QPushButton("Sharpness")
  30. btn_bw = QPushButton("B/W")
  31.  
  32. #### LAYOUT TIME! ####
  33.  
  34. row = QHBoxLayout()          # Main line
  35. col1 = QVBoxLayout()         # divided into two columns
  36. col2 = QVBoxLayout()
  37.  
  38. col1.addWidget(btn_dir)      # First column: Directory selection button
  39. col1.addWidget(lw_files)     # and file list
  40.  
  41. col2.addWidget(lb_image, 95) # Second column - image
  42.  
  43. row_tools = QHBoxLayout()    # Horizontal widgets (all filter buttons)
  44. row_tools.addWidget(btn_left)
  45. row_tools.addWidget(btn_right)
  46. row_tools.addWidget(btn_flip)
  47. row_tools.addWidget(btn_sharp)
  48. row_tools.addWidget(btn_bw)
  49. col2.addLayout(row_tools)    
  50.  
  51. row.addLayout(col1, 20)      # Stretching factors of the columns
  52. row.addLayout(col2, 80)      # When you expand, it will keep this ratio
  53. win.setLayout(row)
  54.  
  55. workdir = ''                         # Blank directory
  56.  
  57. def filter(files, extensions):       # Arguments for filter: files, extension
  58.    result = []                       # Empty list for results
  59.    for filename in files:               # For every filename inside files;
  60.        for ext in extensions:           # For every extension in extensions;
  61.            if filename.endswith(ext):       # if the filename has an extension
  62.                result.append(filename)      # Add the filename to the result list
  63.    return result                            # return value of result
  64.  
  65. def chooseWorkdir():                 # Set global working directory
  66.    global workdir
  67.    workdir = QFileDialog.getExistingDirectory()
  68.  
  69. def showFilenamesList():             # Show list of filenames in List Widget (lw)
  70.    extensions = ['.jpg','.jpeg', '.png', '.gif', '.bmp']
  71.    chooseWorkdir()
  72.    filenames = filter(os.listdir(workdir), extensions)
  73.    lw_files.clear()                  # Empty the widget
  74.    for filename in filenames:
  75.        lw_files.addItem(filename)    # Add items from selected folder
  76.  
  77. btn_dir.clicked.connect(showFilenamesList)  # Connect the button so it works
  78.  
  79. '''Create an image processor CLASS'''
  80. class ImageProcessor():
  81.    def __init__(self):
  82.        self.image = None
  83.        self.dir = None
  84.        self.filename = None
  85.        self.save_dir = "Modified/"
  86.  
  87.    def loadImage(self, dir, filename):
  88.        '''When loading, remember the path and file name'''
  89.        self.dir = dir
  90.        self.filename = filename
  91.        image_path = os.path.join(dir, filename)
  92.        self.image = Image.open(image_path)
  93.  
  94.    def showImage(self, path):
  95.        lb_image.hide()
  96.        pixmapimage = QPixmap(path)
  97.        w, h = lb_image.width(), lb_image.height()
  98.        pixmapimage = pixmapimage.scaled(w, h, Qt.KeepAspectRatio)
  99.        lb_image.setPixmap(pixmapimage)
  100.        lb_image.show()
  101.  
  102. workimage = ImageProcessor()
  103.  
  104. '''Program it to show thumbnails'''
  105. def showChosenImage():
  106.    if lw_files.currentRow() >= 0:
  107.        filename = lw_files.currentItem().text()
  108.        workimage.loadImage(workdir, filename)
  109.        image_path = os.path.join(workimage.dir, workimage.filename)
  110.        workimage.showImage(image_path)
  111.  
  112. lw_files.currentRowChanged.connect(showChosenImage)
  113.  
  114. # Show the interface! Done :)
  115. win.show()
  116. app.exec()
  117.  
  118. '''No filters work yet because these are just thumbnails'''
Advertisement
Add Comment
Please, Sign In to add comment