Page 1 of 1

Python - ClipThumbnail to QPixMap?

PostPosted: Wed Jan 13, 2021 10:11 am
by sbjork
I'm trying to convert the thumbnail data to a QPixMap, but without success. I see that OpenCV is used in the example files, but shouldn't I be able to use PySide for this?

Here's some example code that I expected to work:

Code: Select all
import sys
from PySide import QtCore, QtGui

class Example(QtGui.QWidget):

   def __init__(self):
      super(Example, self).__init__()

      self.lab = QtGui.QLabel()      

      # Use for testing to see that everything works.
      #self.img = QtGui.QImage("path/to/image.png")

      self.img = QtGui.QImage()
      img_data = self.get_img_data()
      bytesarr = QtCore.QByteArray.fromBase64(img_data)
      self.img.loadFromData(bytesarr, 'PNG')
      
      pix = QtGui.QPixmap.fromImage(self.img)
      self.lab.setPixmap(pix)

      layout = QtGui.QVBoxLayout()
      layout.addWidget(self.lab)
      self.setLayout(layout)

   def get_img_data(self):

      # Get a Resolve object
      # Update with your own code.
      resolve = get_resolve()

      pm = resolve.GetProjectManager()
      proj = pm.GetCurrentProject()
      tl = proj.GetCurrentTimeline()

      # Clip under time indicator.
      clip = tl.GetCurrentVideoItem()

      if not clip:
         print "No clip under time indicator."
         return

      thumb = tl.GetCurrentClipThumbnailImage()
      thumb_data = thumb["data"]

      return thumb_data


app = QtGui.QApplication(sys.argv)
ex = Example()
ex.show()
app.exec_()

Re: Python - ClipThumbnail to QPixMap?

PostPosted: Wed Jan 13, 2021 10:52 am
by sbjork
For fututre reference, this seems to work:

Code: Select all
import sys
import base64
from PySide import QtCore, QtGui


class Example(QtGui.QWidget):

   def __init__(self):
      super(Example, self).__init__()

      self.lab = QtGui.QLabel()

      self.thumb = self.get_thumbnail()
      self.lab.setPixmap(self.thumb)

      layout = QtGui.QVBoxLayout()
      layout.addWidget(self.lab)
      self.setLayout(layout)

   def get_thumbnail(self):

      '''
      Return the current clip as a Qpixmap.
      '''

      # Get a Resolve object
      # Update with your own code.
      resolve = get_resolve()

      pm = resolve.GetProjectManager()
      proj = pm.GetCurrentProject()
      tl = proj.GetCurrentTimeline()

      # Timeline and item under time indicator.
      clip = tl.GetCurrentVideoItem()

      if not clip:
         print "No clip under time indicator."
         return

      thumb = tl.GetCurrentClipThumbnailImage()
      data = thumb["data"]
      w = thumb["width"]
      h = thumb["height"]

      bytesarr = QtCore.QByteArray.fromBase64(data)
      bytes_per_line = 3 * w
      qimg = QtGui.QImage(bytesarr, w, h, bytes_per_line, QtGui.QImage.Format_RGB888)

      pix = QtGui.QPixmap.fromImage(qimg)

      return pix

def main():

   app = QtGui.QApplication(sys.argv)
   ex = Example()
   ex.show()
   app.exec_()

if __name__ == '__main__':
   main()