Advertisement
shh_algo_PY

Jayden - Easy Editor - Part 4

May 20th, 2022
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.58 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. from PyQt5.QtCore import Qt # For Qt.KeepAspectRatio constant to resize while maintaining proportions
  11. from PyQt5.QtGui import QPixmap # screen-optimisation (pixel map)
  12. from PIL import Image # For all the filters
  13.  
  14. # Make the window
  15. app = QApplication([])
  16. win = QWidget()      
  17. win.resize(700, 500)
  18. win.setWindowTitle('Easy Editor')
  19. lb_image = QLabel("Image")
  20. btn_dir = QPushButton("Folder")
  21. lw_files = QListWidget()
  22.  
  23. # Make all the buttons
  24. btn_left = QPushButton("Left")
  25. btn_right = QPushButton("Right")
  26. btn_flip = QPushButton("Mirror")
  27. btn_sharp = QPushButton("Sharpness")
  28. btn_bw = QPushButton("B/W")
  29.  
  30. #### LAYOUT TIME! ####
  31.  
  32. row = QHBoxLayout()          # Main line
  33. col1 = QVBoxLayout()         # divided into two columns
  34. col2 = QVBoxLayout()
  35.  
  36. col1.addWidget(btn_dir)      # First column: Directory selection button
  37. col1.addWidget(lw_files)     # and file list
  38.  
  39. col2.addWidget(lb_image, 95) # Second column - image
  40.  
  41. row_tools = QHBoxLayout()    # Horizontal widgets (all filter buttons)
  42. row_tools.addWidget(btn_left)
  43. row_tools.addWidget(btn_right)
  44. row_tools.addWidget(btn_flip)
  45. row_tools.addWidget(btn_sharp)
  46. row_tools.addWidget(btn_bw)
  47. col2.addLayout(row_tools)    
  48.  
  49. row.addLayout(col1, 20)      # Stretching factors of the columns
  50. row.addLayout(col2, 80)      # When you expand, it will keep this ratio
  51. win.setLayout(row)
  52.  
  53. workdir = ''                         # Blank directory
  54.  
  55. def filter(files, extensions):       # Arguments for filter: files, extension
  56.    result = []                       # Empty list for results
  57.    for filename in files:               # For every filename inside files;
  58.        for ext in extensions:           # For every extension in extensions;
  59.            if filename.endswith(ext):       # if the filename has an extension
  60.                result.append(filename)      # Add the filename to the result list
  61.    return result                            # return value of result
  62.  
  63. def chooseWorkdir():                 # Set global working directory
  64.    global workdir
  65.    workdir = QFileDialog.getExistingDirectory()
  66.  
  67. def showFilenamesList():             # Show list of filenames in List Widget (lw)
  68.    extensions = ['.jpg','.jpeg', '.png', '.gif', '.bmp']
  69.    chooseWorkdir()
  70.    filenames = filter(os.listdir(workdir), extensions)
  71.    lw_files.clear()                  # Empty the widget
  72.    for filename in filenames:
  73.        lw_files.addItem(filename)    # Add items from selected folder
  74.  
  75. btn_dir.clicked.connect(showFilenamesList)  # Connect the button so it works
  76.  
  77. class ImageProcessor():
  78.    def __init__(self):
  79.        self.image = None
  80.        self.dir = None
  81.        self.filename = None
  82.        self.save_dir = "Modified/"
  83.  
  84.    def loadImage(self, dir, filename):
  85.        '''When loading, remember the path and file name'''
  86.        self.dir = dir
  87.        self.filename = filename
  88.        image_path = os.path.join(dir, filename)
  89.        self.image = Image.open(image_path)
  90.  
  91. #================ ADD THESE ====================#
  92.  
  93.    def do_bw(self):
  94.        # File conversion
  95.        self.image = self.image.convert("L")
  96.        self.saveImage()
  97.        image_path = os.path.join(self.dir, self.save_dir, self.filename)
  98.        self.showImage(image_path)
  99.  
  100.    def saveImage(self):
  101.        # Saves edited image as a copy
  102.        path = os.path.join(self.dir, self.save_dir)
  103.        if not(os.path.exists(path) or os.path.isdir(path)):
  104.            os.mkdir(path)
  105.        image_path = os.path.join(path, self.filename)
  106.        self.image.save(image_path)
  107.  
  108. #================== UNTIL HERE ====================#
  109.  
  110.    def showImage(self, path):
  111.        lb_image.hide()
  112.        pixmapimage = QPixmap(path)
  113.        w, h = lb_image.width(), lb_image.height()
  114.        pixmapimage = pixmapimage.scaled(w, h, Qt.KeepAspectRatio)
  115.        lb_image.setPixmap(pixmapimage)
  116.        lb_image.show()
  117.  
  118. workimage = ImageProcessor() # CURRENT IMAGE
  119.  
  120. def showChosenImage():
  121.    if lw_files.currentRow() >= 0:
  122.        filename = lw_files.currentItem().text()
  123.        workimage.loadImage(workdir, filename)
  124.        image_path = os.path.join(workimage.dir, workimage.filename)
  125.        workimage.showImage(image_path)
  126.  
  127. '''Let's make one black and white!'''
  128.  
  129. lw_files.currentRowChanged.connect(showChosenImage) # from previous code [YOU HAVE THIS]
  130.  
  131. btn_bw.clicked.connect(workimage.do_bw) # Add a button connect the BW function [ADD THIS]
  132.  
  133. # Show the interface! Done :)
  134. win.show()
  135. app.exec()
  136.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement