'Display Image from URL

I am trying to display an image from a URL in pyside6, but can't get anything to work.

def getIcon(data):
    iconID = data['weather'][0]['icon']
    icon = QPixmap("http://openweathermap.org/img/w/" + iconID + ".png");
    return icon

And

self.temperatureIcon = QtWidgets.QLabel(self).setPixmap(getIcon(self.weatherData))

is the code I have.



Solution 1:[1]

Here's another simple solution\example:

import sys
import requests
from PySide6.QtGui     import QPixmap, QScreen
from PySide6.QtWidgets import QApplication, QWidget, QLabel


URL = 'https://upload.wikimedia.org/wikipedia/en/7/7d/Lenna_(test_image).png'


class App(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()
 
    def initUI(self):
        self.setWindowTitle('getAndSetImageFromURL()')
        self.label  = QLabel(self)
        self.pixmap = QPixmap()
        self.getAndSetImageFromURL(URL)
        self.resize(self.pixmap.width(),self.pixmap.height())
        screenSize = QScreen.availableGeometry(QApplication.primaryScreen())
        frmX = (screenSize.width () - self.width ())/2
        frmY = (screenSize.height() - self.height())/2
        self.move(frmX, frmY)
        self.show() 
    
    def getAndSetImageFromURL(self,imageURL):
        request = requests.get(imageURL)
        self.pixmap.loadFromData(request.content)
        self.label.setPixmap(self.pixmap)
        #QApplication.processEvents() # uncoment if executed on loop
    
      
if __name__ == '__main__':
    app = QApplication(sys.argv)
    gui = App()
    sys.exit(app.exec())
which outputs:
enter image description here

Solution 2:[2]

import sys
import requests

import PySide6
from PySide6.QtNetwork import QNetworkAccessManager, QNetworkRequest, QNetworkReply
from PySide6.QtWidgets import QTableView, QWidget, QApplication, QGridLayout, QHeaderView
from PySide6.QtCore import Qt, QAbstractTableModel, QObject, Signal, QUrl
from PySide6.QtGui import QColor, QIcon, QPixmap, QImage

from datetime import datetime

    class MagicIcon():
        def __init__(self, link):
            self.link = link
            self.icon = QIcon()
            try:
                response = requests.get(self.link)
                pixmap = QPixmap()
                pixmap.loadFromData(response.content)
                self.icon = QIcon(pixmap)
            except:
                pass

    
    class MainWindow(QWidget):
        def __init__():
            super().__init__()
            self.setWindowIcon(MagicIcon(
                    "https://img.icons8.com/external-flatarticons-blue-flatarticons/65/000000/external-analysis-digital-marketing-flatarticons-blue-flatarticons-1.png"
            ).icon)
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        wid = MainWindow()
        wid.show()
        sys.exit(app.exec())

Use requests module to fetch image and store them.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2