-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.py
382 lines (306 loc) · 12.4 KB
/
ui.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import sys, os, signal
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout, QFileDialog, QHBoxLayout, QFrame, QPushButton, QSizePolicy, QTextEdit, QSlider, QComboBox, QCheckBox
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap, QMovie, QImage
from PIL import Image
from datetime import datetime
import transf
import model_finder as mf
import ui_inpainting as inp
import more_windows as mw
signal.signal(signal.SIGINT, signal.SIG_DFL)
LOADING_IMAGE = os.path.join(os.path.dirname(__file__), "reload-cat.gif")
signal.signal(signal.SIGINT, signal.SIG_DFL) # Support du CTRL+C
BASE_IMAGE = None
MASK_IMAGE = None
class ImageLabel(QLabel):
def __init__(self):
super().__init__()
self.setAlignment(Qt.AlignCenter)
self.setText('\n\n Déposez l\'image ici ou cliquez pour la téléverser \n\n')
self.setStyleSheet('''
QLabel{
border: 4px dashed #d62828
}
''')
self.movie = None # Maintient anim chargement
self.pixmap_image = None
self.fi_size = self.size()
print("c",self.size())
def setLoadingAnimation(self):
if os.path.exists(LOADING_IMAGE):
self.movie = QMovie(LOADING_IMAGE)
self.setMovie(self.movie)
self.movie.start()
self.repaint()
else:
print(f"Error: Loading GIF not found at {LOADING_IMAGE}")
def clearLoadingAnimation(self):
if self.movie is not None:
self.movie.stop()
self.setMovie(None)
def setPixmap(self, image):
self.clearLoadingAnimation()
self.pixmap_image = image
scaled_pixmap = image.scaled(self.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation)
super().setPixmap(scaled_pixmap)
self.fi_size = self.size()
def updatePixmap(self):
# Redimention des images en conservant le ratio
scaled_pixmap = self.pixmap_image.scaled(self.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation)
super().setPixmap(scaled_pixmap)
self.fi_size = self.size()
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
file_path, _ = QFileDialog.getOpenFileName(self, "Choisissez une image", "", "Images (*.png *.xpm *.jpg *.jpeg *.bmp)")
if file_path:
self.setLoadingAnimation()
new_f = self.transform_image(file_path)
self.setPixmap(QPixmap(new_f))
def transform_image(self, file_path):
if not os.path.exists("./outputs"):
os.makedirs("./outputs")
global BASE_IMAGE
BASE_IMAGE = file_path
theApp.original_window.setPixmap(QPixmap(BASE_IMAGE))
theApp.original_window.show()
theApp.repaint()
theApp.buttonInpainting.show()
theApp.buttonRedo.show()
new_f = "./outputs/"+ str(datetime.now()) + os.path.basename(file_path)
if theApp.inpaint_window != None:
if theApp.inpaint_window.CheckUse.isChecked():
transf.inpaint(file_path, new_f, MASK_IMAGE, prompt1.toPlainText(), prompt2.toPlainText(), slider.value()/100, model=theApp.combo1.currentText())
return new_f
transf.transform(file_path, new_f, prompt1.toPlainText(), prompt2.toPlainText(), slider.value()/100, model=theApp.combo1.currentText())
return new_f
class App(QWidget):
def __init__(self):
global prompt1, prompt2, slider, width, height
super().__init__()
screen = QApplication.primaryScreen()
screen_size = screen.availableGeometry()
width = screen_size.width()
height = screen_size.height()
# self.resize(width, height) # fullscreen
self.resize(800,600)
#self.setFixedSize(self.size()) # blockage plein écran
self.setWindowTitle("Mixtura")
self.setAcceptDrops(True)
self.isExtended = False
mainLayout = QHBoxLayout()
self.setStyleSheet("background-color: #1e1616;")
frame = QFrame(self)
frame.setFrameShape(QFrame.Box)
self.quickSettingsframe = QFrame(self)
self.quickSettingsframe.setFrameShape(QFrame.Box)
quickSettingsframeLayout = QVBoxLayout(self.quickSettingsframe)
self.quickSettingsframe.setStyleSheet("background-color: #282020;border: 0px solid #ccc;")
self.quickSettingsframe.hide()
label1 = QLabel(self)
label1.setText("Que souhaitez-vous obtenir ?")
quickSettingsframeLayout.addWidget(label1)
prompt1 = QTextEdit(self)
quickSettingsframeLayout.addWidget(prompt1)
prompt1.setStyleSheet('''
QTextEdit {
border: 2px solid rgba(255, 0, 0, 0.5);
border-radius: 5px;
}
''')
label2 = QLabel(self)
label2.setText("Que souhaitez-vous éviter ?")
quickSettingsframeLayout.addWidget(label2)
prompt2 = QTextEdit(self)
quickSettingsframeLayout.addWidget(prompt2)
prompt2.setStyleSheet('''
QTextEdit {
border: 2px solid rgba(255, 0, 0, 0.5);
border-radius: 5px;
}
''')
label3 = QLabel(self)
label3.setText("Intensité de modification de l'image")
quickSettingsframeLayout.addWidget(label3)
label4 = QFrame(self)
label4.setFrameShape(QFrame.Box)
label4Layout = QHBoxLayout(label4)
label4.setStyleSheet("background-color: #282020;border: 0px solid #ccc;")
label4a = QLabel(self)
label4a.setText("Ne pas modifier")
label4Layout.addWidget(label4a)
label4b = QLabel(self)
label4b.setText("Modifier totalement")
label4Layout.addWidget(label4b)
quickSettingsframeLayout.addWidget(label4)
slider = QSlider(Qt.Horizontal, self)
# Apply custom styles
slider.setStyleSheet('''
QSlider::groove:horizontal {
height: 5px;
background: #282020;
border: 2px solid rgba(255, 0, 0, 0.5);
border-radius: 5px;
}
QSlider::handle:horizontal {
background: #282020;
border: 5px solid rgba(255, 0, 0, 0.5);
width: 10px;
height: 10px;
margin-top: -10px;
margin-bottom: -10px;
cursor: pointer;
}
''')
slider.setRange(0, 100)
slider.setValue(50)
slider.setSingleStep(5)
quickSettingsframeLayout.addWidget(slider)
label5 = QLabel(self)
label5.setText("Quel style désirez-vous ?")
quickSettingsframeLayout.addWidget(label5)
self.combo1 = QComboBox()
self.combo1.setStyleSheet('''
QComboBox {
border: 2px solid rgba(255, 0, 0, 0.5);
border-radius: 5px;
}
''')
quickSettingsframeLayout.addWidget(self.combo1)
for ele in mf.getAllModelsNameAndTag():
self.combo1.addItem(ele)
self.combo1.currentTextChanged.connect(self.on_combobox1_changed)
"""
label6 = QLabel(self)
label6.setText("Souhaitez-vous utiliser un sous-style ?")
quickSettingsframeLayout.addWidget(label6)
self.combo2 = QComboBox()
quickSettingsframeLayout.addWidget(self.combo2)
self.combo2.setStyleSheet('''
QComboBox {
border: 2px solid rgba(255, 0, 0, 0.5);
border-radius: 5px;
}
''')
self.combo2.addItem("Non")
self.combo2.addItem("Non-")
"""
self.buttonRedo = QPushButton("Traiter l'image")
self.buttonRedo.clicked.connect(self.onRedoClicked)
self.buttonRedo.setStyleSheet('''
QPushButton {
border: 2px solid rgba(255, 0, 0, 0.5);
border-radius: 5px;
}
''')
self.photoViewer = ImageLabel()
frameLayout = QVBoxLayout(frame)
frameLayout.addWidget(self.photoViewer)
frame.setStyleSheet('border: 0px solid #ccc;')
# Le bouton vertical permettant d'afficher/cacher le menu
self.buttonExtend = QPushButton("ᐸ")
self.buttonExtend.setStyleSheet('''
QPushButton {
background-color: #282020;
color: red;
padding: 10px;
font-size: 14px;
border-radius: 5px;
}
QPushButton:hover {
background-color: #3e5f99;
}
''')
# On étends le bouton
self.buttonExtend.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Expanding) # Expension verticale
self.buttonExtend.clicked.connect(self.onExpandClicked)
self.buttonLayout = QHBoxLayout()
self.buttonInpainting = QPushButton("Éditer une zone")
self.buttonInpainting.setStyleSheet('''
QPushButton {
border: 2px solid rgba(255, 0, 0, 0.5);
border-radius: 5px;
}
''')
self.buttonInpainting.hide()
self.buttonRedo.hide()
self.buttonInpainting.clicked.connect(self.onInpaintClicked)
self.buttonLayout.addWidget(self.buttonRedo)
self.buttonLayout.addWidget(self.buttonInpainting)
frameLayout.addLayout(self.buttonLayout)
self.original_window = mw.OriginalWindow()
self.original_window.hide()
self.inpaint_window = None
mainLayout.addWidget(self.quickSettingsframe)
mainLayout.addWidget(self.buttonExtend)
mainLayout.addWidget(frame)
self.setLayout(mainLayout)
def onInpaintClicked(self):
global MASK_IMAGE
inpaintingWindow = inp.InpaintingApp(BASE_IMAGE)
inpaintingWindow.exec_()
MASK_IMAGE = inpaintingWindow.mask_image
self.inpaint_window = mw.InpaintWindow()
self.inpaint_window.show()
self.inpaint_window.labelInpaint.setPixmap(inpaintingWindow.display_pixmap)
self.inpaint_window.CheckUse.setChecked(True)
def onExpandClicked(self):
if self.isExtended:
self.quickSettingsframe.hide()
self.isExtended = False
self.buttonExtend.setText("ᐸ")
else:
self.quickSettingsframe.show()
self.isExtended = True
self.buttonExtend.setText("ᐳ")
def onRedoClicked(self):
if BASE_IMAGE != None:
self.set_image(BASE_IMAGE)
def on_combobox1_changed(self, value):
transf.change_pipeline(mf.models[mf.getAllModelsNameAndTag()[value]])
def process_images_in_folder(self, folder_path):
supported_formats = ('.png', '.xpm', '.jpg', '.jpeg', '.bmp')
for file_name in os.listdir(folder_path):
self.photoViewer.setLoadingAnimation()
if file_name.lower().endswith(supported_formats):
file_path = os.path.join(folder_path, file_name)
new_f = self.photoViewer.transform_image(file_path)
self.photoViewer.setPixmap(QPixmap(new_f))
self.repaint()
def dragEnterEvent(self, event):
if event.mimeData().hasImage:
event.setDropAction(Qt.CopyAction)
file_path = event.mimeData().urls()[0].toLocalFile()
self.set_image(file_path)
event.accept()
if event.mimeData().hasUrls():
for url in event.mimeData().urls():
file_path = url.toLocalFile()
if os.path.isdir(file_path):
print(f"Dropped a folder: {file_path}")
self.process_images_in_folder(file_path)
else:
event.ignore()
def dragMoveEvent(self, event):
if event.mimeData().hasImage:
event.accept()
else:
event.ignore()
def dropEvent(self, event):
print(event)
if event.mimeData().hasImage:
event.setDropAction(Qt.CopyAction)
file_path = event.mimeData().urls()[0].toLocalFile()
self.set_image(file_path)
event.accept()
else:
event.ignore()
def set_image(self, file_path):
self.photoViewer.setLoadingAnimation()
new_f = self.photoViewer.transform_image(file_path)
self.photoViewer.setPixmap(QPixmap(new_f))
if __name__ == '__main__':
app = QApplication(sys.argv)
theApp = App()
theApp.show()
sys.exit(app.exec_())