I have a task - to display text on the image. I first tried with Pillow, but I did not like it. It has a very bad and inconvenient API and cannot kindly output multi-line text. So here I am trying to do Qt now:

img = QImage('input.jpg') painter = QPainter(img) painter.setPen(QPen(Qt.red)) painter.setFont(QFont('Arial', 12, QFont.Bold)) painter.drawText(img.rect(), Qt.AlignCenter, 'Hello world!') painter.end() img.save('output.jpg') 

But when it comes to painter.drawText error Windows crashes. "Python program terminated." In detail, the Qt5Gui.dll module is Qt5Gui.dll . What does it mean?

  • @Yami he has a constructor that accepts a QPaintDevice object. I suppose this is an alternative to calling the begin method. Although tried and cause - the same effect. - PECHAPTER
  • @Yami I say with a call to begin, the error does not disappear! Besides, the error falls on drawText - I debugged it. It still does not reach the end call. - PECHAPTER
  • Is that all your program code? - mkkik
  • @mkkik yes. This is a demo project, I'm just testing for now. It is a minimally reproducible example. - PECHAURIST
  • one
    Then, the problem is that you don't have a QGuiApplication instance in QGuiApplication . A segmentation error occurs in the QGuiApplication::font() function. - mkkik

1 answer 1

mkkik:

Then, the problem is that you don't have a QGuiApplication instance in runtime. Segmentation error occurs in QGuiApplication :: font () function

Running code:

 from PyQt5.QtGui import QGuiApplication, QImage, QPainter, QPen, QFont from PyQt5.QtCore import Qt app = QGuiApplication([]) img = QImage(200, 200, QImage.Format_RGB32) img.fill(Qt.white) painter = QPainter(img) painter.setPen(QPen(Qt.red)) painter.setFont(QFont('Arial', 12, QFont.Bold)) painter.drawText(img.rect(), Qt.AlignCenter, 'Hello world!') painter.end() img.save('output.jpg') 

output.jpg:

enter image description here