Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在QImage顶部创建一个透明网格_Python_Pyqt_Pyqt5 - Fatal编程技术网

Python 在QImage顶部创建一个透明网格

Python 在QImage顶部创建一个透明网格,python,pyqt,pyqt5,Python,Pyqt,Pyqt5,我正在尝试构建一个具有网格的绘画应用程序,但我不希望网格包含在保存的照片中。 我试图画一个透明的图像,但我得到了一个黑色的背景,而不是 以下是完整的代码: import sys from PyQt5 import QtGui, QtWidgets, QtCore from PyQt5.QtCore import QPoint, Qt from PyQt5.QtGui import QBrush, QGuiApplication, QImage, QPainter, QPen, QIcon, Q

我正在尝试构建一个具有网格的绘画应用程序,但我不希望网格包含在保存的照片中。 我试图画一个透明的图像,但我得到了一个黑色的背景,而不是

以下是完整的代码:

import sys

from PyQt5 import QtGui, QtWidgets, QtCore
from PyQt5.QtCore import QPoint, Qt
from PyQt5.QtGui import QBrush, QGuiApplication, QImage, QPainter, QPen, QIcon, QColor
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QTextEdit, QAction, QFileDialog,
    QGraphicsScene, QGraphicsProxyWidget, QGraphicsView

import pyautogui
import matplotlib.pyplot as plt




class Drawer(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        self._drawing = False
        self.last_point = QPoint()

        self.image_layer = QImage(self.size(), QImage.Format_RGB32)
        self.image_layer.fill(Qt.gray)

        self.brushSize = 2
        self.brushColor = Qt.black

        #paint = QPainter(self.image_layer)
        #paint.setCompositionMode(QtGui.QPainter.CompositionMode_Clear)

        # paint.drawLine(0,0,self.size().width(),0)
        # paint.drawLine(0,10,200,10)

        #paint.drawLine(0,0,0,200)
        #paint.drawLine(10,0,10,200)
        self.update()


    def mousePressEvent(self, event):
        self._drawing = True
        self.last_point = event.pos()

    def mouseMoveEvent(self, event):
        if self._drawing and event.buttons() & Qt.LeftButton:
            painter = QPainter(self.image_layer)
            painter.setPen(
                QPen(
                    self.brushColor,
                    self.brushSize,
                    Qt.SolidLine,
                    Qt.RoundCap,
                    Qt.RoundJoin,
                )
            )
            painter.drawLine(self.last_point, event.pos())
            self.last_point = event.pos()
            self.update()

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.drawImage(QPoint(), self.image_layer)
        painter.end()


    def resizeEvent(self, event):
        if (
                self.size().width() > self.image_layer.width()
                or self.size().height() > self.image_layer.height()
        ):
            qimg = QImage(
                max(self.size().width(), self.image_layer.width()),
                max(self.size().height(), self.image_layer.height()),
                QImage.Format_RGB32,
            )
            qimg.fill(Qt.gray)
            painter = QPainter(qimg)
            painter.drawImage(QPoint(), self.image_layer)
            painter.drawLine(0, 0, qimg.size().width(), 0)
            painter.drawLine(0, 10, qimg.size().width(), 10)
            painter.drawLine(0, 600, qimg.size().width(), 600)
            print(qimg.size().height())
            painter.end()
            self.image_layer = qimg
            self.update()

class Window(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)

        [x, y] = pyautogui.size()
        self.setGeometry(0, 0, x, y)

        self.drawer = Drawer()
        textbox = QTextEdit("Converted text will show here")

        central_widget = QWidget()
        self.setCentralWidget(central_widget)

        vlay = QVBoxLayout(central_widget)
        vlay.addWidget(textbox)
        vlay.addWidget(self.drawer, stretch=1)

        mainMenu = self.menuBar()
        fileMenu = mainMenu.addMenu("File")

        saveAction = QAction(QIcon("icons/save.png"), "Save", self)
        saveAction.setShortcut("Ctrl+S")
        fileMenu.addAction(saveAction)
        saveAction.triggered.connect(self.save)


    def save(self):
        filePath, _ = QFileDialog.getSaveFileName(self.drawer, "Save Image", "",
                                                  "PNG(*.png);;JPEG(*.jpg *.jpeg);;All Files(*.*) ")

        if filePath == "":
            return
        self.drawer.image_layer.save(filePath)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec())


注意:只有两条线模拟网格,我可以稍后绘制,但现在我希望保存的图像不包括研磨线。

您应该在paint Event中绘制网格,而不是在图像上连续绘制网格

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.drawImage(QPoint(), self.image_layer)

        gridSize = 10
        x = y = 0
        width = self.width()
        height = self.height()
        while y <= height:
            # draw horizontal lines
            painter.drawLine(0, y, width, y)
            y += gridSize
        while x <= width:
            # draw vertical lines
            painter.drawLine(x, 0, x, height)
            x += gridSize
def paintEvent(自,事件):
油漆工=油漆工(自身)
painter.drawImage(QPoint(),self.image_层)
网格大小=10
x=y=0
宽度=self.width()
高度=自身高度()

尽管如此,请始终尝试使您的代码尽可能。所有与手头的事情无关的事情(在你的例子中有很多)都会分散注意力,让回答问题变得比应该的更困难。我会的。谢谢。我已经把这个问题做得最少,可以重复,并且删除了不相关的代码。我能告诉你原因吗?您在painter.drawImage()中使用的是there image,那么有什么区别以及为什么网格没有显示在保存的pic上?@FarouK当一个小部件要显示时,Qt会向它发送一个绘制事件,
paintEvent
负责向用户实际“显示”它自己。使用标准小部件(按钮、组合等)的
paintEvent()
“绘制”小部件的基本实现。QWidget是一个“空”小部件,因此不会绘制任何内容,但如果您将其子类化并覆盖其
paintEvent()
,则可以绘制任何需要的内容。出现的东西不是永久性的:它只是用户在屏幕上实际看到的东西。因此,您可以使用它在GUI中显示网格和QImage,但这不会更改QImage内容。