1

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.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
tgrandje
  • 2,332
  • 11
  • 33

1 Answers1

3

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_())

enter image description here

Libraries:

  • requests 2.18.4
  • PyQt4 4.12.1
  • Python 2.7.14 and Python 3.6.4

It is always good to verify the appropriate formats, for this you must use:

print(QImageReader.supportedImageFormats())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Sorry, that didn't change anything : _assert img.loadFromData(r.content)_ is raising an error, because it isn't able to load any data from bytes. This seems to confirm my suspicions there may be some bug around here. I'll try to update my packages first and let you know... – tgrandje Apr 11 '18 at 18:52
  • @tgrandje I have added the versions of my libraries, what versions do you have? – eyllanesc Apr 11 '18 at 18:56
  • I just updated my post with it. As you're using python 2.7, I'm starting to think this is coming from using python 3.6 _(I've learned the hard way some parts of PyQt4 needs to be handled differently when upgrading from 27 to 3.6)_ – tgrandje Apr 11 '18 at 19:03
  • @tgrandje I've checked with both. – eyllanesc Apr 11 '18 at 19:07
  • I just tried with python 2.7 and it works correctly indeed – tgrandje Apr 11 '18 at 19:09
  • @tgrandje execute: `print(QImageReader.supportedImageFormats())` – eyllanesc Apr 11 '18 at 19:09
  • @tgrandje or print(type(r.content)) – eyllanesc Apr 11 '18 at 19:10
  • You're right, there is some anomally here, this version of PyQt4 is somehow depleted (only bmp, pbm, pgm, png, ppm, xbm, xpm). Thanks ! (You should probably edit your answer before I accept it.) – tgrandje Apr 11 '18 at 19:15
  • @tgrandje read this: `http://article.gmane.org/gmane.comp.python.pyqt-pykde/7176/match=jpeg+plugin+not+loading` – eyllanesc Apr 11 '18 at 19:18
  • Works correctly for me with binaries from https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyqt4. `supportedImageFormats` returns bmp, gif, ico, jpeg, jpg, mng, pbm, pgm, png, ppm, svg, svgz, tga, tif, tiff, xbm, xpm – cgohlke Apr 12 '18 at 00:31
  • Make sure `qt.conf`exists and is correct, and that there are no conflicting QT* DLLs in the Windows DLL search path. – cgohlke Apr 12 '18 at 00:32
  • @cgohlke : thanks for your complements. I suspect it has something to do with conflicts between PyQt4 & PyQt5 on my WinPython, or the QApplication's instance (mentionned in the article eyllanesc reported earlier)... Instead of diving to deep in this problems (which seems way out of my league, whatever this is), I finally took the leap to PyQt5... – tgrandje Apr 12 '18 at 16:28