gpt4 book ai didi

python - PyQt:如何使用自定义消息生成光栅图像

转载 作者:行者123 更新时间:2023-12-01 05:21:39 25 4
gpt4 key购买 nike

要生成我正在使用的纯灰色图像:

def generate_image(filepath):
img = QtGui.QImage(720, 405, QtGui.QImage.Format_RGB32)
img.fill(QtCore.Qt.gray)
img.save(filepath, 'JPG')

我想知道自定义此功能以将简单的文本消息叠加在灰色背景上会有多困难。感谢您的意见!

最佳答案

您只需要使用 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')

关于python - PyQt:如何使用自定义消息生成光栅图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22186544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com