python - 在 QImage 上绘制矩形而不显示它

标签 python python-3.x pyqt pyqt5 qimage

我想在 QImage 上方绘制矩形并将结果保存为 png。下面的最小示例应该可以做到这一点。

from PyQt5.QtGui import QImage, QColor, QPainter, QBrush
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget

import sys

class Window(QMainWindow):

    def __init__(self):
        super().__init__()
        self.title = "PyQt5 Drawing Tutorial"
        self.top = 150
        self.left = 150
        self.width = 500
        self.height = 500
        self.mypainter = MyPainter()
        self.mypainter.create()
        self.InitWindow()
    def InitWindow(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.top, self.left, self.width, self.height)
        self.show()

class MyPainter(QWidget):
    def __init__(self):
        super().__init__()
        self.img = QImage(25, 25, QImage.Format_RGBA64)
        self.color1 = QColor(255, 0, 0, 255)
        self.color2 = QColor(0, 255, 0, 255)
        self.color3 = QColor(0, 0, 255, 255)
        self.boxes = (
            (2, 2, 10, 10),
            (5, 5, 4, 5),
            (10, 10, 10, 7))

    def create(self):
        self.colors = (
            self.color1,
            self.color2,
            self.color3)
        for idx,  box in enumerate(self.boxes):
            self.color = self.colors[idx]
            self.bndboxSize = box
            self.repaint()
        self.img.save("myImg.png")

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.drawImage(self.rect(), self.img)
        painter.setBrush(QBrush(self.color))
        painter.drawRect(self.bndboxSize)

App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())

我的预期

enter image description here

黑色背景也可以是透明的。

我得到了什么

enter image description here

这只是一个带有 alpha channel 的图像,如果您将鼠标悬停,您将获得链接

我得到了什么( Debug模式)

enter image description here

我不知道如何获得想要的图像。

最佳答案

如果你想创建一个图像,那么没有必要使用小部件,但你必须使用 QPainter 在 QImage 上绘画。在OP的尝试中,它被绘制在QWidget上,而QImage只有噪音。

from PyQt5.QtCore import QRect, Qt
from PyQt5.QtGui import QColor, QGuiApplication, QImage, QPainter


def create_image():
    img = QImage(25, 25, QImage.Format_RGBA64)
    img.fill(Qt.black)

    painter = QPainter(img)

    colors = (QColor(255, 0, 0, 255), QColor(0, 255, 0, 255), QColor(0, 0, 255, 255))
    boxes = (QRect(2, 2, 10, 10), QRect(5, 5, 4, 5), QRect(10, 10, 10, 7))

    for color, box in zip(colors, boxes):
        painter.fillRect(box, color)

    painter.end()

    return img


def main():
    app = QGuiApplication([])
    qimage = create_image()
    qimage.save("myImg.png")


if __name__ == "__main__":
    main()

输出:

enter image description here

关于python - 在 QImage 上绘制矩形而不显示它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63158561/

相关文章:

python - 如何在所有存储在一个大列表中的列表中搜索匹配项

python - 我应该如何随机生成异常值?

python - 获取多列并将它们放入与 Pandas 相同的索引中

python - 优化tensorflow数据集api中的洗牌缓冲区大小

python-3.x - 如何从h2标签获取HREF? Python/ Selenium

python - 将焦点放在 QlineEdit 小部件上

python - 将 Cython 标记为构建依赖项?

python - 如何在 django admin 中添加编辑和删除按钮

Python 无法引用调用类中的列表

python - 如何在另一个函数中使用 QDateEdit 小部件中设置的用户值?