This repository has been archived on 2020-08-22. You can view files and clone it, but cannot push or open issues or pull requests.
pyaudviz/preview_thread.py

80 lines
2.4 KiB
Python
Raw Normal View History

from PySide2 import QtCore, QtGui
from PySide2.QtCore import Signal, Slot
2015-03-02 16:47:52 -05:00
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 = Signal(['QImage'])
2015-03-02 16:47:52 -05:00
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
@Slot(str, str, QtGui.QFont, int, int, int, int, tuple, tuple)
def createPreviewImage(self, backgroundImage, titleText, titleFont, fontSize,\
alignment, xOffset, yOffset, textColor, visColor):
2015-03-02 16:47:52 -05:00
# print('worker thread id: {}'.format(QtCore.QThread.currentThreadId()))
dic = {
"backgroundImage": backgroundImage,
"titleText": titleText,
2017-04-18 07:35:29 -04:00
"titleFont": titleFont,
2017-04-20 13:01:19 -04:00
"fontSize": fontSize,
2017-04-20 12:47:14 -04:00
"alignment": alignment,
"xoffset": xOffset,
"yoffset": yOffset,
"textColor" : textColor,
"visColor" : visColor
2015-03-02 16:47:52 -05:00
}
self.queue.put(dic)
@Slot()
2015-03-02 16:47:52 -05:00
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]
2015-03-02 16:47:52 -05:00
im = self.core.drawBaseImage(
bgImage,
2015-03-02 16:47:52 -05:00
nextPreviewInformation["titleText"],
2017-04-18 07:35:29 -04:00
nextPreviewInformation["titleFont"],
2017-04-20 13:01:19 -04:00
nextPreviewInformation["fontSize"],
2017-04-20 12:47:14 -04:00
nextPreviewInformation["alignment"],
nextPreviewInformation["xoffset"],
nextPreviewInformation["yoffset"],
nextPreviewInformation["textColor"],
nextPreviewInformation["visColor"])
2015-03-02 16:47:52 -05:00
spectrum = numpy.fromfunction(lambda x: 0.008*(x-128)**2, (255,), dtype="int16")
im = self.core.drawBars(spectrum, im, nextPreviewInformation["visColor"])
2015-03-02 16:47:52 -05:00
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