Issue
I'm very new to Qt (specifically PySide) and I'm trying write a script that loads an animated gif from file into a QByteArray and then into a QMovie. The reason for going from file to the QByteArray is because I cannot keep that gif file in memory. I want to be able to store the animated gif in such a way that it can be written out to a JSON file later (hence the QByteArray). I've tried using ekhumoro's answer from here and although no errors showed up, the animated gif also doesn't show up. (There could be something there but I don't see anything.) My code, in a nutshell, looks like this:
data = open("img.gif", "rb").read()
self.bArray = QtCore.QByteArray(data)
self.bBuffer = QtCore.QBuffer(self.bArray)
self.bBuffer.open(QtCore.QIODevice.ReadOnly)
self.movie = QtGui.QMovie(self.bBuffer, 'GIF')
self.movieLabel.setMovie(self.movie) # a QLabel
self.movie.start()
I want to store the contents of self.bArray
to a JSON file later.
I can see the animated gif when I give the QMovie constructor the file path but then I won't be able to save the contents of the gif to a JSON file.
I'm wondering if the data is not being read in properly or not being passed to QMovie properly.
Any ideas?
Thanks!
Solution
This looks like a PySide bug, as the same code works perfectly fine in PyQt.
The bug seems to be in the QMovie
constructor, which does not read anything from the device passed to it. A work-around is to set the device explicitly, like this:
import sys
from PySide import QtCore, QtGui
# from PyQt4 import QtCore, QtGui
app = QtGui.QApplication(sys.argv)
data = open('anim.gif', 'rb').read()
a = QtCore.QByteArray(data)
b = QtCore.QBuffer(a)
print('open: %s' % b.open(QtCore.QIODevice.ReadOnly))
m = QtGui.QMovie()
m.setFormat('GIF')
m.setDevice(b)
print('valid: %s' % m.isValid())
w = QtGui.QLabel()
w.setMovie(m)
m.start()
w.resize(500, 500)
w.show()
app.exec_()
print('pos: %s' % b.pos())
Answered By - ekhumoro
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.