要生成我正在使用的纯灰色图像:
def generate_image(filepath):
img = QtGui.QImage(720, 405, QtGui.QImage.Format_RGB32)
img.fill(QtCore.Qt.gray)
img.save(filepath, 'JPG')
我想知道自定义此功能以在灰色背景上层叠简单文本消息是多么困难。我很感激输入!
答案 0 :(得分:2)
您只需使用QPainter在图片上绘图:
def generate_image(filepath, text='Hello World!', color='red', font=None):
img = QtGui.QImage(720, 405, QtGui.QImage.Format_RGB32)
img.fill(QtCore.Qt.gray)
painter = QtGui.QPainter(img)
painter.setPen(QtGui.QColor(color))
if font is None:
font = painter.font()
font.setBold(True)
font.setPointSize(24)
painter.setFont(font)
painter.drawText(img.rect(), QtCore.Qt.AlignCenter, text)
painter.end()
return img.save(filepath, 'JPG')