'How get icon from url and set to windowIcon

When I try to set my icon from url which contain favicon.ico it work fine, but when I try to set my icon from url which contain **.jpg or .png ** format it does not work and return none. Kindly help me to solve the problem.

you can check the following code.

 
 # url = "http://www.google.com/favicon.icon
         
 url = 'http://www.geo.tv/assets/front/images/gn-icons/256x256.jpg'
 self.nam.finished.connect(self.finishRequest)
 self.nam.get(QNetworkRequest(QUrl(url)))
 



def finishRequest(self,reply):
        img = QImage()
        
        img.loadFromData(reply.readAll())
        self.setWindowIcon(QIcon(img))

I want to get url of image(png,jpg) mean url of icon of a website. and set to a label, windowIcon etc. but it works only with image(.ico) format and does not work with other format and return nothing.



Solution 1:[1]

Analyzing the url I have seen that redirection from http to https so the reply does not have the bytes of the image but the following url. One possible solution is to have Qt handle redirects using request.setAttribute(QNetworkRequest.FollowRedirectsAttribute, True).

import sys
from functools import cached_property

from PySide2.QtCore import Signal, QObject, Qt, QUrl
from PySide2.QtGui import QImage, QPixmap, QIcon
from PySide2.QtNetwork import QNetworkAccessManager, QNetworkReply, QNetworkRequest
from PySide2.QtWidgets import QApplication, QWidget


class ImageDownloader(QObject):
    finished = Signal(QImage)

    def __init__(self, parent=None):
        super().__init__(parent)
        self.manager.finished.connect(self.handle_finished)

    @cached_property
    def manager(self):
        return QNetworkAccessManager()

    def start_download(self, url):
        request = QNetworkRequest(url)
        request.setAttribute(QNetworkRequest.FollowRedirectsAttribute, True)
        self.manager.get(request)

    def handle_finished(self, reply):
        if reply.error() != QNetworkReply.NoError:
            print("error: ", reply.errorString())
            return
        image = QImage()
        ok = image.loadFromData(reply.readAll())
        if not ok:
            print("error")
            return
        self.finished.emit(image)


class Widget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.downloader = ImageDownloader()
        self.downloader.finished.connect(self.handle_finished)
        url = QUrl("http://www.geo.tv/assets/front/images/gn-icons/256x256.jpg")
        self.downloader.start_download(url)

        self.resize(640, 480)

    def handle_finished(self, image):
        pixmap = QPixmap.fromImage(image)
        self.setWindowIcon(QIcon(pixmap))


def main():
    app = QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()

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