viewing paste Unknown #58688 | Text

Posted on the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QApplication, QWidget,
                             QVBoxLayout, QHBoxLayout,
                             QPushButton, QLabel,
                             QListWidget, QFileDialog)
from PyQt5.QtGui import QPixmap
from functools import partial
import os
from PIL import Image, ImageOps
 
 
app = QApplication([])
window = QWidget()
window.setWindowTitle("Редактор")
window.setFixedSize(700, 400) # resize(700, 400)
 
lb_image = QLabel("Картинка")
btn_dir = QPushButton("Папка")
lw_files = QListWidget()
 
btn_left = QPushButton("Лево")
btn_right = QPushButton("Право")
btn_flip = QPushButton("Зеркало")
btn_sharp = QPushButton("Резкость")
btn_bw = QPushButton("Ч/Б")
btn_save = QPushButton("Сохранить")
btn_reset = QPushButton("Сброс")
 
row = QHBoxLayout()
col1 = QVBoxLayout()
col2 = QVBoxLayout()
row1_tools = QHBoxLayout()
row2_tools = QHBoxLayout()
 
col1.addWidget(btn_dir)
col1.addWidget(lw_files)
 
col2.addWidget(lb_image, stretch=95)
 
row1_tools.addWidget(btn_left)
row1_tools.addWidget(btn_right)
row1_tools.addWidget(btn_flip)
row1_tools.addWidget(btn_sharp)
row1_tools.addWidget(btn_bw)
 
row2_tools.addWidget(btn_save)
row2_tools.addWidget(btn_reset)
 
col2.addLayout(row1_tools)
col2.addLayout(row2_tools)
 
row.addLayout(col1, stretch=20)
row.addLayout(col2, stretch=80)
 
window.setLayout(row)
 
# from PIL import Image
 
class ImageProcessor():
    def __init__(self):
        self.image = None
        self.dir = None
        self.filename = None
        self.save_dir = "Modified/"
 
    def load_image(self, dir, filename):
        self.dir = dir
        self.filename = filename
        image_path = os.path.join(dir, filename) # Downloads/original.jpg
        self.image = Image.open(image_path)
 
    def show_image(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()
 
    def do_bw(self):
        self.image = ImageOps.grayscale(self.image)
        self.save_image()
        image_path = os.path.join(self.dir, self.save_dir, self.filename)
        self.show_image(image_path)
 
    def save_image(self):
        path = os.path.join(self.dir, self.save_dir)
        if not (os.path.exists(path) or os.path.isdir(path)):
            os.mkdir(path)
        image_path = os.path.join(path, self.filename)
        self.image.save(image_path)
 
 
workimage = ImageProcessor()
 
def show_chosen_image(workdir, index):
    if index >= 0:
        filename = lw_files.currentItem().text()
        workimage.load_image(workdir, filename)
        image_path = os.path.join(workimage.dir, workimage.filename)
        workimage.show_image(image_path)
 
def choose_workdir():
    try:
        workdir = QFileDialog.getExistingDirectory()
        if workdir:
            return workdir
        else:
            print("Папка не выбрана")
            return None
    except:
        return None
 
def filter(files):
    result = []
    extension = [".png", ".jpg", ".jpeg", ".gif", ".bmp"]
    for file in files:
        for ext in extension:
            if file.endswith(ext): # endswith("original.jpg") -> .jpg -> []
                result.append(file)
    return result
 
def show_filenames():
    dir = choose_workdir()
    files = filter(os.listdir(dir)) # ["name.docs", "1.xlsx", "2.pptx", "original.jpg"]
    lw_files.clear()
    for file in files:
        lw_files.addItem(file)
 
    lw_files.currentRowChanged.connect(partial(show_chosen_image, dir))
 
btn_bw.clicked.connect(workimage.do_bw)
btn_dir.clicked.connect(show_filenames)
 
window.show()
app.exec_()
Viewed 573 times, submitted by Guest.