Untitled

 avatar
unknown
plain_text
2 days ago
3.3 kB
5
Indexable
from PyQt5.QtWidgets import (
   QApplication, QWidget,
   QFileDialog, # Діалог відкриття файлів (і папок)
   QLabel, QPushButton, QListWidget,
   QHBoxLayout, QVBoxLayout
)

from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import Qt

import os
from PIL import Image
from PIL import ImageFilter

app = QApplication([])
win = QWidget()       
win.resize(700, 500) 
win.setWindowTitle('Easy Editor')

lb_image = QLabel("Картинка")
btn_dir = QPushButton("Папка")
lw_files = QListWidget()


btn_left = QPushButton("Вліво")
btn_right = QPushButton("Вправо")
btn_flip = QPushButton("Дзеркало")
btn_sharp = QPushButton("Різкість")
btn_bw = QPushButton("Ч/Б")


row = QHBoxLayout()          # Основний рядок
col1 = QVBoxLayout()         # ділиться на два стовпці
col2 = QVBoxLayout()
col1.addWidget(btn_dir)      # у першому – кнопка вибору директорії
col1.addWidget(lw_files)     # та список файлів
col2.addWidget(lb_image, 95) # у другому - картинка
row_tools = QHBoxLayout()    # та рядок кнопок
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)
row.addLayout(col2, 80)
win.setLayout(row)



class ImageProcessor:
    def __init__(self):
        self.image = None #поточне зображення (за замовчуванням None
        self.file_name = None
        self.save_dir = "Modified/"
    
    def load_image(self,filename):
        self.filename = filename
        image_path = os.path.join(workdir, filename)
        self.image = Image.open(image_path)
        print("image path:", 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()



workdir = ""

extensions = [
    ".png",
    ".jpg",
    ".gif",
    ".jpeg",
    ".bmp"
]

def filter(extensions, filenames):
    result = []
    for f in filenames:
        for ext in extensions:
            if f.endswith(ext):
                result.append(f)
                
    return result


def chooseworkdir():
    global workdir
    workdir = QFileDialog.getExistingDirectory()
    print("workdir: ", workdir)

def showFileNames():
    chooseworkdir()
    
    if workdir:
        filenames = os.listdir(workdir)
        filenames = filter(extensions, filenames)
        print("filenames", filenames)
        
        for f in filenames:
            lw_files.addItem(f)


def showChosenImage():
    if lw_files.currentRow() >= 0:
        filename = lw_files.currentItem().text()
        image_processor.load_image(filename)
        image_path = os.path.join(workdir, filename)
        image_processor.showImage(image_path)

image_processor = ImageProcessor()


btn_dir.clicked.connect(showFileNames)
lw_files.currentRowChanged.connect(showChosenImage)

win.show()
app.exec()
Editor is loading...
Leave a Comment