1

how to add a text on image? It may place top right top left bottom right bottom left or center it does no matter?

class Example(QWidget):

    def __init__(self):
       super().__init__()

    self.im = QPixmap("./images.png")
    self.label = QLabel()
    # self.label.setText("sdsdsd")
    # self.label.setStyleSheet("color:black")

    self.label.setPixmap(self.im)

    self.grid = QGridLayout()
    self.grid.addWidget(self.label,1,1)
    self.setLayout(self.grid)

    self.setGeometry(50,50,320,200)
    self.setWindowTitle("\n")
    self.show()

if name == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

here is what i did but i could not a text on image. How can solve this? I googled many times but I did not find any solutions regarding this issue.

coder
  • 441
  • 1
  • 4
  • 19

2 Answers2

3

Try it:

import sys
from PyQt5.QtCore import *
from PyQt5.QtGui  import *
from PyQt5.QtWidgets import *

class Label(QLabel):
    def __init__(self):
        super().__init__()

    def paintEvent(self, e):
        qp = QPainter()
        qp.begin(self)

        image  = QImage('im.png')
        qp.drawImage(QPoint(), image)

        pen = QPen(Qt.red)
        pen.setWidth(2)
        qp.setPen(pen)        

        font = QFont()
        font.setFamily('Times')
        font.setBold(True)
        font.setPointSize(24)
        qp.setFont(font)

        qp.drawText(150, 250, "Hello World !")

        qp.end()


class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.setGeometry(50, 50, 660, 620)
        self.setWindowTitle("Add a text on image")

        self.label = Label() 

        self.grid = QGridLayout()
        self.grid.addWidget(self.label)
        self.setLayout(self.grid)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec_())

enter image description here

S. Nick
  • 12,879
  • 8
  • 25
  • 33
1

You can use opencv to put text on image. i modified your code with cv2

import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import cv2
img = cv2.imread('/path/to/image/download.jpeg')
cv2.putText(img, "sample_text", (50,20), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 1)
cv2.imwrite("download.jpeg",img)
class Example(QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.im = QPixmap("./download.jpeg")

        self.label = QLabel()
        self.label.setPixmap(self.im)
        self.grid = QGridLayout()
        self.grid.addWidget(self.label,1,1)
        self.setLayout(self.grid)
        #self.title.setMinimumHeight(self.pixmap.height())

        self.setGeometry(50,50,320,200)
        #self.setText("hai")
        self.setWindowTitle("\n")
        self.show()

if __name__ == '__main__':

    app = QApplication(sys.argv)
    window = Example()
    window.setGeometry(600, 100, 200, 30)
    window.show()
    sys.exit(app.exec_())

(or)

Use this answer to get some idea:

How to add both an image and text to a QLabel

akash
  • 779
  • 5
  • 16