forked from djfun/audio-visualizer-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpreview_thread.py
More file actions
79 lines (67 loc) · 2.44 KB
/
Copy pathpreview_thread.py
File metadata and controls
79 lines (67 loc) · 2.44 KB
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
from PyQt4 import QtCore, QtGui, uic
from PyQt4.QtCore import pyqtSignal, pyqtSlot
from PIL import Image, ImageDraw, ImageFont
from PIL.ImageQt import ImageQt
import core
import time
from queue import Queue, Empty
import numpy
class Worker(QtCore.QObject):
imageCreated = pyqtSignal(['QImage'])
def __init__(self, parent=None, queue=None):
QtCore.QObject.__init__(self)
parent.newTask.connect(self.createPreviewImage)
parent.processTask.connect(self.process)
self.core = core.Core()
self.queue = queue
@pyqtSlot(str, str, QtGui.QFont, int, int, int, int, tuple, tuple)
def createPreviewImage(self, backgroundImage, titleText, titleFont, fontSize,\
alignment, xOffset, yOffset, textColor, visColor):
# print('worker thread id: {}'.format(QtCore.QThread.currentThreadId()))
dic = {
"backgroundImage": backgroundImage,
"titleText": titleText,
"titleFont": titleFont,
"fontSize": fontSize,
"alignment": alignment,
"xoffset": xOffset,
"yoffset": yOffset,
"textColor" : textColor,
"visColor" : visColor
}
self.queue.put(dic)
@pyqtSlot()
def process(self):
try:
nextPreviewInformation = self.queue.get(block=False)
while self.queue.qsize() >= 2:
try:
self.queue.get(block=False)
except Empty:
continue
bgImage = self.core.parseBaseImage(\
nextPreviewInformation["backgroundImage"],
preview=True
)
if bgImage == []:
bgImage = ''
else:
bgImage = bgImage[0]
im = self.core.drawBaseImage(
bgImage,
nextPreviewInformation["titleText"],
nextPreviewInformation["titleFont"],
nextPreviewInformation["fontSize"],
nextPreviewInformation["alignment"],
nextPreviewInformation["xoffset"],
nextPreviewInformation["yoffset"],
nextPreviewInformation["textColor"],
nextPreviewInformation["visColor"])
spectrum = numpy.fromfunction(lambda x: 0.008*(x-128)**2, (255,), dtype="int16")
im = self.core.drawBars(spectrum, im, nextPreviewInformation["visColor"])
self._image = ImageQt(im)
self._previewImage = QtGui.QImage(self._image)
self._scaledPreviewImage = self._previewImage.scaled(320, 180, QtCore.Qt.IgnoreAspectRatio, QtCore.Qt.SmoothTransformation)
self.imageCreated.emit(self._scaledPreviewImage)
except Empty:
True