Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- from PyQt5.QtWidgets import (
- QApplication, QWidget,
- QFileDialog, # Dialogue for opening files (and folders)
- QLabel, QPushButton, QListWidget,
- QHBoxLayout, QVBoxLayout
- )
- '''Adding libraries'''
- from PyQt5.QtCore import Qt
- # For Qt.KeepAspectRatio constant to resize while maintaining proportions
- from PyQt5.QtGui import QPixmap # screen-optimisation (pixel map)
- from PIL import Image # For all the image properties
- # Make the window
- app = QApplication([])
- win = QWidget()
- win.resize(700, 500)
- win.setWindowTitle('Easy Editor')
- lb_image = QLabel("Image")
- btn_dir = QPushButton("Folder")
- lw_files = QListWidget()
- # Make all the buttons
- btn_left = QPushButton("Left")
- btn_right = QPushButton("Right")
- btn_flip = QPushButton("Mirror")
- btn_sharp = QPushButton("Sharpness")
- btn_bw = QPushButton("B/W")
- #### LAYOUT TIME! ####
- row = QHBoxLayout() # Main line
- col1 = QVBoxLayout() # divided into two columns
- col2 = QVBoxLayout()
- col1.addWidget(btn_dir) # First column: Directory selection button
- col1.addWidget(lw_files) # and file list
- col2.addWidget(lb_image, 95) # Second column - image
- row_tools = QHBoxLayout() # Horizontal widgets (all filter buttons)
- row_tools.addWidget(btn_left)
- row_tools.addWidget(btn_right)
- row_tools.addWidget(btn_flip)
- row_tools.addWidget(btn_sharp)
- row_tools.addWidget(btn_bw)
- col2.addLayout(row_tools)
- row.addLayout(col1, 20) # Stretching factors of the columns
- row.addLayout(col2, 80) # When you expand, it will keep this ratio
- win.setLayout(row)
- workdir = '' # Blank directory
- def filter(files, extensions): # Arguments for filter: files, extension
- result = [] # Empty list for results
- for filename in files: # For every filename inside files;
- for ext in extensions: # For every extension in extensions;
- if filename.endswith(ext): # if the filename has an extension
- result.append(filename) # Add the filename to the result list
- return result # return value of result
- def chooseWorkdir(): # Set global working directory
- global workdir
- workdir = QFileDialog.getExistingDirectory()
- def showFilenamesList(): # Show list of filenames in List Widget (lw)
- extensions = ['.jpg','.jpeg', '.png', '.gif', '.bmp']
- chooseWorkdir()
- filenames = filter(os.listdir(workdir), extensions)
- lw_files.clear() # Empty the widget
- for filename in filenames:
- lw_files.addItem(filename) # Add items from selected folder
- btn_dir.clicked.connect(showFilenamesList) # Connect the button so it works
- '''Create an image processor CLASS'''
- class ImageProcessor():
- def __init__(self):
- self.image = None
- self.dir = None
- self.filename = None
- self.save_dir = "Modified/"
- def loadImage(self, dir, filename):
- '''When loading, remember the path and file name'''
- self.dir = dir
- self.filename = filename
- image_path = os.path.join(dir, filename)
- self.image = Image.open(image_path)
- def showImage(self, path):
- lb_image.hide()
- pixmapimage = QPixmap(path)
- w, h = lb_image.width(), lb_image.height()
- pixmapimage = pixmapimage.scaled(w, h, Qt.KeepAspectRatio)
- lb_image.setPixmap(pixmapimage)
- lb_image.show()
- workimage = ImageProcessor()
- '''Program it to show thumbnails'''
- def showChosenImage():
- if lw_files.currentRow() >= 0:
- filename = lw_files.currentItem().text()
- workimage.loadImage(workdir, filename)
- image_path = os.path.join(workimage.dir, workimage.filename)
- workimage.showImage(image_path)
- lw_files.currentRowChanged.connect(showChosenImage)
- # Show the interface! Done :)
- win.show()
- app.exec()
- '''No filters work yet because these are just thumbnails'''
Advertisement
Add Comment
Please, Sign In to add comment