'How to add image to QTextBrowser (PyQt4) using Python at a specific required point?

I am doing Text mining using python. I created a GUI using QT and transformed it into python snippet using PyQt4. I have QTextBrowser which indicates the sentiment index of the passage. Based on the score of the sentiment index I want to add different images into the Qtextbrowser. Below which I have added the screenshot of my tool.

https://drive.google.com/file/d/0B-bFVFevEa-yMUZUMnc2UWN4T00/edit?usp=sharing

Below image is the screenshot of the tool which I have created for Text mining. At the the bottom of the right hand panel , you can see the score of sentiment of the passage. Now I want to add the image on the highlighted square box area depending upon the score of the sentiment.

How to add the image at the particular point in the QtextBrowser ?



Solution 1:[1]

http://pyqt.sourceforge.net/Docs/PyQt4/qtextbrowser.html#loadResource

Example:

import sys
from PyQt4 import QtGui, QtCore

class myTextBrowser(QtGui.QTextBrowser):
    def loadResource (self, type, name):
        return QtGui.QPixmap("test.jpg")

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    a = myTextBrowser()
    a.document().setHtml("""My image :<br /><img src="test.jpg"/>""")
    a.show()
    sys.exit(app.exec_())

Solution 2:[2]

import sys
import requests
from PyQt6 import QtGui, QtCore

class myTextBrowser(QtGui.QTextBrowser):
    def loadResource (self, type, name):
        pic_data = requests.get(name.url(), allow_redirects=True).content
        pixmap = QPixmap()
        pixmap.loadFromData(pic_data)
        return pixmap

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    a = myTextBrowser()
    a.setHtml("""My image :<br /><img src="img_url"/>""")
    a.show()
    sys.exit(app.exec_())

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 wbt11a
Solution 2 Toha