forked from njbbaer/progrockdiffusion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprdgui.py
111 lines (82 loc) · 2.88 KB
/
prdgui.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
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QPixmap, QImage
from PyQt5.QtCore import QObject, QThread, QTimer, pyqtSignal
from PIL import Image
import sys
class Window(QMainWindow):
def __init__(self, worker_func, width, height):
super().__init__()
self.acceptDrops()
# set the title
self.setWindowTitle("progrockdiffusion")
# setting the geometry of window
self.setGeometry(500, 500, width, height)
# creating label
self.label = QLabel(self)
# loading image
self.pixmap = QPixmap(width, height)
# adding image to label
self.label.setPixmap(self.pixmap)
#self.label.setText('')
# Optional, resize label to image size
self.label.resize(self.pixmap.width(), self.pixmap.height())
self.worker_func = worker_func
QTimer.singleShot(0, self.runWorker)
# show all the widgets
self.show()
def runWorker(self):
# Step 2: Create a QThread object
self.thread = QThread()
# Step 3: Create a worker object
self.worker = Worker(self.worker_func)
# Step 4: Move worker to the thread
self.worker.moveToThread(self.thread)
# Step 5: Connect signals and slots
self.thread.started.connect(self.worker.run)
self.worker.finished.connect(self.thread.quit)
self.worker.finished.connect(self.worker.deleteLater)
self.thread.finished.connect(self.thread.deleteLater)
self.worker.finished.connect(self.close)
#self.worker.progress.connect(self.reportProgress)
# Step 6: Start the thread
self.thread.start()
def setPixmap(self, img):
pixmap = pil2pixmap(img)
self.label.setPixmap(pixmap)
def setText(self, txt):
self.label.setText(txt)
window = None
class Worker(QObject):
finished = pyqtSignal()
progress = pyqtSignal(QPixmap)
def __init__(self, worker_func):
super().__init__()
self.func = worker_func
def run(self):
self.func()
self.finished.emit()
def run_gui(worker_func, width, height):
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
global window
window = Window(worker_func, width, height)
# start the app
sys.exit(App.exec())
def update_image(img):
window.setPixmap(img)
def pil2pixmap(im):
if im.mode == "RGB":
r, g, b = im.split()
im = Image.merge("RGB", (b, g, r))
elif im.mode == "RGBA":
r, g, b, a = im.split()
im = Image.merge("RGBA", (b, g, r, a))
elif im.mode == "L":
im = im.convert("RGBA")
# Bild in RGBA konvertieren, falls nicht bereits passiert
im2 = im.convert("RGBA")
data = im2.tobytes("raw", "RGBA")
qim = QImage(data, im.size[0], im.size[1], QImage.Format_ARGB32)
pixmap = QPixmap.fromImage(qim)
return pixmap