N
N
Nur Kasimov2016-06-30 13:26:40
Python
Nur Kasimov, 2016-06-30 13:26:40

How to display a downloaded image from the Internet without intermediate saving to disk?

The program downloads a picture from the Internet using the requests library and displays it in a QLabel on the form.
Now the program does it like this ( python 3.5 ):

if res.status_code == 200:
    path = "images/"+trek+"#1"+".jpg"
    with open(path, "wb") as w:
        w.write(res.content)
    img1.setPixmap(QtGui.QPixmap(path))

those. saves the image and then opens it. How to display an image immediately without saving it to disk?
I found a similar question on the site, but not in the python language and I am a beginner, I could not figure it out. Tried different options. Help a noob.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sim3x, 2016-06-30
@luchs

try like this

# python2
# from StringIO import StringIO

from io import StringIO

if res.status_code == 200:
    p = QtGui.QPixmap()
    p.loadFromData(StringIO(res.content))
    img1.setPixmap(p)

or use /tmp/

A
Alexey Cheremisin, 2016-06-30
@leahch

Well, you can pass a picture from memory - QPixmap::QPixmap(const char * const[] xpm)
Only before that you need to convert the image into an xpm array.
You can also: 1) first create a QImage::QImage(uchar * data, int width, int height, Format format)
from a stream of bytes 2) then convert the resulting QImage into a QPixmap bool QPixmap::convertFromImage(const QImage & image, Qt:: ImageConversionFlags flags = Qt::AutoColor) 3) pass it to img1.setPixmap

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question