preview_thread.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. from PyQt5 import QtCore, QtGui, uic
  2. from PyQt5.QtCore import pyqtSignal, pyqtSlot
  3. from PIL import Image, ImageDraw, ImageFont
  4. from PIL.ImageQt import ImageQt
  5. import core
  6. import time
  7. from queue import Queue, Empty
  8. import numpy
  9. class Worker(QtCore.QObject):
  10. imageCreated = pyqtSignal(['QImage'])
  11. def __init__(self, parent=None, queue=None):
  12. QtCore.QObject.__init__(self)
  13. parent.newTask.connect(self.createPreviewImage)
  14. parent.processTask.connect(self.process)
  15. self.core = core.Core()
  16. self.queue = queue
  17. @pyqtSlot(str, str, QtGui.QFont, int, int, int, int, tuple, tuple)
  18. def createPreviewImage(self, backgroundImage, titleText, titleFont, fontSize,\
  19. alignment, xOffset, yOffset, textColor, visColor):
  20. # print('worker thread id: {}'.format(QtCore.QThread.currentThreadId()))
  21. dic = {
  22. "backgroundImage": backgroundImage,
  23. "titleText": titleText,
  24. "titleFont": titleFont,
  25. "fontSize": fontSize,
  26. "alignment": alignment,
  27. "xoffset": xOffset,
  28. "yoffset": yOffset,
  29. "textColor" : textColor,
  30. "visColor" : visColor
  31. }
  32. self.queue.put(dic)
  33. @pyqtSlot()
  34. def process(self):
  35. try:
  36. nextPreviewInformation = self.queue.get(block=False)
  37. while self.queue.qsize() >= 2:
  38. try:
  39. self.queue.get(block=False)
  40. except Empty:
  41. continue
  42. bgImage = self.core.parseBaseImage(\
  43. nextPreviewInformation["backgroundImage"],
  44. preview=True
  45. )
  46. if bgImage == []:
  47. bgImage = ''
  48. else:
  49. bgImage = bgImage[0]
  50. im = self.core.drawBaseImage(
  51. bgImage,
  52. nextPreviewInformation["titleText"],
  53. nextPreviewInformation["titleFont"],
  54. nextPreviewInformation["fontSize"],
  55. nextPreviewInformation["alignment"],
  56. nextPreviewInformation["xoffset"],
  57. nextPreviewInformation["yoffset"],
  58. nextPreviewInformation["textColor"],
  59. nextPreviewInformation["visColor"])
  60. spectrum = numpy.fromfunction(lambda x: 0.008*(x-128)**2, (255,), dtype="int16")
  61. im = self.core.drawBars(spectrum, im, nextPreviewInformation["visColor"])
  62. self._image = ImageQt(im)
  63. self._previewImage = QtGui.QImage(self._image)
  64. self._scaledPreviewImage = self._previewImage.scaled(320, 180, QtCore.Qt.IgnoreAspectRatio, QtCore.Qt.SmoothTransformation)
  65. self.imageCreated.emit(self._scaledPreviewImage)
  66. except Empty:
  67. True