-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
168 lines (133 loc) · 4.73 KB
/
main.py
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import os
from PyQt5.QtWidgets import (
QApplication, QWidget,
QFileDialog,
QLabel, QPushButton, QListWidget,
QHBoxLayout, QVBoxLayout
)
from PyQt5.QtCore import Qt # нужна константа Qt.KeepAspectRatio для изменения размеров с сохранением пропорций
from PyQt5.QtGui import QPixmap # оптимизированная для показа на экране картинка
from PIL import Image
from PIL.ImageQt import ImageQt # для перевода графики из Pillow в Qt
from PIL import ImageFilter
from PIL.ImageFilter import (
BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE,
EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN,
GaussianBlur, UnsharpMask
)
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)
win.show()
workdir = ''
def filter(files, extensions):
result = []
for filename in files:
for ext in extensions:
if filename.endswith(ext):
result.append(filename)
return result
def chooseWordir():
global workdir
workdir = QFileDialog.getExistingDirectory()
def showFilenamesList():
extensions = ['.jpg', '.jpeg', '.png', 'gif', ".bmp"]
chooseWorkdir()
filenames = filter(os.listdir(workdir), extensions)
lw_files.clear()
for filename in filenames:
lw_files.addItem(filename)
btn_dir.clicked.connect(showFilenamesList)
class ImageProcessor():
def __init__(self):
self.image = None
self.dir = None
self.filename = None
self.save_dir = 'Polina/'
def loadImage(self, filename):
self.filename = filename
fullname = os.path.join(workdir,filename)
self.image = Image.open(fullname)
def saveImage(self):
path = os.path.join(workdir, self.save_dir)
if not(os.path.exists(path) or os.path.isdir(path)):
os.mkdir(path)
fullname = os.path.join(path, self.filename)
self.image.save(fullname)
def do_bw(self):
self.image = self.image.convert('L')
self.saveImage()
image_path = os.path.join(workdir,
self.save_dir, self.filename)
self.showImage(image_path)
def do_left(self):
self.image = self.image.transpose(Image.ROTATE_90)
self.saveImage()
image_path = os.path.join(workdir,
self.save_dir, self.filename)
self.showImage(image_path)
def do_right(self):
self.image = self.image.transpose(Image.ROTATE_270)
self.saveImage()
image_path = os.path.join(workdir,
self.save_dir, self.filename)
self.showImage(image_path)
def do_flip(self):
self.image = self.image.transpose(Image.FLIP_LEFT_RIGHT)
self.saveImage()
image_path = os.path.join(workdir,
self.save_dir, self.filename)
self.showImage(image_path)
def do_sharpen(self):
self.image = self.image.filter(SHARPEN)
self.saveImage()
image_path = os.path.join(workdir,
self.save_dir, self.filename)
self.showImage(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()
def showChosenImage():
if lw_files.currentRow() >= 0:
filename = lw_files.currentItem().text()
workimage.loadImage(filename)
workimage.showImage(os.path.join(workdir,
workimage.filename))
workimage = ImageProcessor()
lw_files.currentRowChanged.connect(showChosenImage)
btn_bw.clicked.connect(workimage.do_bw)
btn_left.clicked.connect(workimage.do_left)
btn_right.clicked.connect(workimage.do_right)
btn_sharp.clicked.connect(workimage.do_sharpen)
btn_flip.clicked.connect(workimage.do_flip)
app.exec()