shh_algo_PY

Easy Editor

Jul 1st, 2022
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.98 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
  13.  
  14. #======================================#
  15. from PIL import ImageFilter
  16. from PIL.ImageQt import ImageQt
  17. from PIL.ImageFilter import (
  18.    BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE,
  19.    EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN,
  20.    GaussianBlur, UnsharpMask
  21. )
  22. #======================================#
  23.  
  24. # Make the window
  25. app = QApplication([])
  26. win = QWidget()      
  27. win.resize(700, 500)
  28. win.setWindowTitle('Easy Editor')
  29. lb_image = QLabel("Image")
  30. btn_dir = QPushButton("Folder")
  31. lw_files = QListWidget()
  32.  
  33. # Make all the buttons
  34. btn_left = QPushButton("Left")
  35. btn_right = QPushButton("Right")
  36. btn_flip = QPushButton("Mirror")
  37. btn_sharp = QPushButton("Sharpness")
  38. btn_bw = QPushButton("B/W")
  39.  
  40. #### LAYOUT TIME! ####
  41.  
  42. row = QHBoxLayout()          # Main line
  43. col1 = QVBoxLayout()         # divided into two columns
  44. col2 = QVBoxLayout()
  45.  
  46. col1.addWidget(btn_dir)      # First column: Directory selection button
  47. col1.addWidget(lw_files)     # and file list
  48.  
  49. col2.addWidget(lb_image, 95) # Second column - image
  50.  
  51. row_tools = QHBoxLayout()    # Horizontal widgets (all filter buttons)
  52. row_tools.addWidget(btn_left)
  53. row_tools.addWidget(btn_right)
  54. row_tools.addWidget(btn_flip)
  55. row_tools.addWidget(btn_sharp)
  56. row_tools.addWidget(btn_bw)
  57. col2.addLayout(row_tools)    
  58.  
  59. row.addLayout(col1, 20)      # Stretching factors of the columns
  60. row.addLayout(col2, 80)      # When you expand, it will keep this ratio
  61. win.setLayout(row)
  62.  
  63. workdir = ''                         # Blank directory
  64.  
  65. def filter(files, extensions):       # Arguments for filter: files, extension
  66.    result = []                       # Empty list for results
  67.    for filename in files:               # For every filename inside files;
  68.        for ext in extensions:           # For every extension in extensions;
  69.            if filename.endswith(ext):       # if the filename has an extension
  70.                result.append(filename)      # Add the filename to the result list
  71.    return result                            # return value of result
  72.  
  73. def chooseWorkdir():                 # Set global working directory
  74.    global workdir
  75.    workdir = QFileDialog.getExistingDirectory()
  76.  
  77. def showFilenamesList():             # Show list of filenames in List Widget (lw)
  78.    extensions = ['.jpg','.jpeg', '.png', '.gif', '.bmp']
  79.    chooseWorkdir()
  80.    filenames = filter(os.listdir(workdir), extensions)
  81.    lw_files.clear()                  # Empty the widget
  82.    for filename in filenames:
  83.        lw_files.addItem(filename)    # Add items from selected folder
  84.  
  85. btn_dir.clicked.connect(showFilenamesList)  # Connect the button so it works
  86.  
  87. class ImageProcessor():
  88.    def __init__(self):
  89.        self.image = None
  90.        self.dir = None
  91.        self.filename = None
  92.        self.save_dir = "Modified/"
  93.  
  94.    def loadImage(self, dir, filename):
  95.        '''When loading, remember the path and file name'''
  96.        self.dir = dir
  97.        self.filename = filename
  98.        image_path = os.path.join(dir, filename)
  99.        self.image = Image.open(image_path)
  100.  
  101.    def do_bw(self):
  102.        # File conversion
  103.        self.image = self.image.convert("L")
  104.        self.saveImage()
  105.        image_path = os.path.join(self.dir, self.save_dir, self.filename)
  106.        self.showImage(image_path)
  107.  
  108. #======================================#
  109.  
  110.    def do_left(self): # LEFT BUTTON
  111.        self.image = self.image.transpose(Image.ROTATE_90)
  112.        self.saveImage()
  113.        image_path = os.path.join(self.dir, self.save_dir, self.filename)
  114.        self.showImage(image_path)
  115.  
  116.    def do_right(self): # RIGHT BUTTON
  117.        self.image = self.image.transpose(Image.ROTATE_270)
  118.        self.saveImage()
  119.        image_path = os.path.join(workdir, self.save_dir, self.filename)
  120.        self.showImage(image_path)
  121.  
  122.    def do_flip(self): # MIRROR BUTTON
  123.        self.image = self.image.transpose(Image.FLIP_LEFT_RIGHT)
  124.        self.saveImage()
  125.        image_path = os.path.join(workdir, self.save_dir, self.filename)
  126.        self.showImage(image_path)
  127.  
  128.    def do_sharpen(self): # SHARPEN BUTTON
  129.        self.image = self.image.filter(SHARPEN)
  130.        self.saveImage()
  131.        image_path = os.path.join(workdir, self.save_dir, self.filename)
  132.        self.showImage(image_path)
  133.  
  134. #======================================#
  135.  
  136.    def saveImage(self):
  137.        # Saves edited image as a copy
  138.        path = os.path.join(self.dir, self.save_dir)
  139.        if not(os.path.exists(path) or os.path.isdir(path)):
  140.            os.mkdir(path)
  141.        image_path = os.path.join(path, self.filename)
  142.        self.image.save(image_path)
  143.  
  144.    def showImage(self, path):
  145.        lb_image.hide()
  146.        pixmapimage = QPixmap(path)
  147.        w, h = lb_image.width(), lb_image.height()
  148.        pixmapimage = pixmapimage.scaled(w, h, Qt.KeepAspectRatio)
  149.        lb_image.setPixmap(pixmapimage)
  150.        lb_image.show()
  151.  
  152. workimage = ImageProcessor() # CURRENT IMAGE
  153.  
  154. def showChosenImage():
  155.    if lw_files.currentRow() >= 0:
  156.        filename = lw_files.currentItem().text()
  157.        workimage.loadImage(workdir, filename)
  158.        image_path = os.path.join(workimage.dir, workimage.filename)
  159.        workimage.showImage(image_path)
  160.  
  161. '''Let's make one black and white!'''
  162.  
  163. lw_files.currentRowChanged.connect(showChosenImage)
  164.  
  165. btn_bw.clicked.connect(workimage.do_bw) # Black and white is done!
  166.  
  167. #======================================#
  168. btn_left.clicked.connect(workimage.do_left)
  169. btn_right.clicked.connect(workimage.do_right)
  170. btn_sharp.clicked.connect(workimage.do_sharpen)
  171. btn_flip.clicked.connect(workimage.do_flip)
  172. #======================================#
  173.  
  174. # Show the interface! Done :)
  175. win.show()
  176. app.exec()
  177.  
Advertisement
Add Comment
Please, Sign In to add comment