Reputation: 2514
I'm trying to load an image (a jpeg in that case from bytes, as it is downloaded from web content). I've already consulted some posts (for ex. 1 & 2), but for some reason I can't reproduce those answers, though JPEG is a wholy implemented format by PyQT4 as stated in the doc.
Let's say the picture to load in a QImage is this one.
I'm first downloading picture using request (though I'm certain this isn't related to this particular problem - I put it here mainly for portability reasons...).
import requests
r = requests.get(href, stream=True)
pict_bytes = r.content
After that, I can check this image is 100% correct using PIL and io modules :
from PIL import Image
import io
image = Image.open(io.BytesIO(pict_bytes))
image.show()
image.save('./test.jpeg')
It gets confusing afterwards, while trying to convert bytes to QImage :
from PyQt4 import QtGui
qim = QtGui.QImage()
qim.loadFromData(pict_bytes)
qim.loadFromData(..) returns False whatever the picture I chose, which I can't understand regarding to the function's description in the doc. I also checked directly from the file :
with open('./test.jpeg', 'rb') as f:
content = f.read()
qim.loadFromData(content)
Is there something obvious that I missed, or is this some strange comportment of PyQt4 with python 3 ? I would be grateful for your insights...
EDIT
I'm starting to believe there is some bug here (responses are all coherent with what I had already tried, in one way or another). Something doesn't feel quite right with the PyQt4 QImage (as well as QPixmap I suspect).
I'm currently using Windows 10 (and used Windows 2008 Server at the office), Winpython 3.6 x64, with PyQt4 4.11.4 installed from the unofficial depositery of Christoph Gohlke.
Upvotes: 1
Views: 4820
Reputation: 243983
Your example is not very clear, but you always have to do the verifications, for this you can use assert
import sys
import requests
from PyQt4.QtGui import QImage, QApplication, QLabel, QPixmap
app = QApplication(sys.argv)
href = "https://upload.wikimedia.org/wikipedia/commons/thumb/6/6f/OrteliusWorldMap.jpeg/800px-OrteliusWorldMap.jpeg"
r = requests.get(href, stream=True)
assert r.status_code == 200
img = QImage()
assert img.loadFromData(r.content)
w = QLabel()
w.setPixmap(QPixmap.fromImage(img))
w.show()
sys.exit(app.exec_())
Libraries:
It is always good to verify the appropriate formats, for this you must use:
print(QImageReader.supportedImageFormats())
Upvotes: 3