pyqt5我保存图像,但它总是空的

时间:2018-01-01 00:23:19

标签: python pyqt pyqt4 pyqt5

我正在使用PyQt5构建一个GUI,在其中我使用类抽屉来让用户使用鼠标进行绘制,但是当我保存图像时它总是空的,任何人都可以告诉我为什么?

class Drawer(QWidget):
   newPoint = pyqtSignal(QPoint)
   def __init__(self, parent=None):
      QWidget.__init__(self, parent)
      self.setAttribute(QtCore.Qt.WA_StaticContents)
      self.modified = False
      self.scribbling = False
      imageSize = QtCore.QSize(9500, 9500)
      h=400
      w=400
      self.myPenWidth = 13
      self.myPenColor = QtCore.Qt.black
      self.image = QtGui.QImage()
      self.image=QtGui.QImage(w,h,QtGui.QImage.Format_RGB32)



      self.path = QPainterPath()   


  def setPenColor(self, newColor):
     self.myPenColor = newColor

  def setPenWidth(self, newWidth):
     self.myPenWidth = newWidth

  def clearImage(self):
     self.path = QPainterPath() 
     self.image.fill(QtGui.qRgb(255, 255, 255))  ## switch it to else 
     self.modified = True
     self.update()


  def saveImage(self, fileName, fileFormat):
     self.image.save(fileName,fileFormat)



  def paintEvent(self, event):
     painter = QPainter(self)

     #painter.setPen(QColor(0, 0, 0))
     painter.setPen(QtGui.QPen(self.myPenColor, 
     self.myPenWidth,QtCore.Qt.SolidLine, QtCore.Qt.RoundCap, 
     QtCore.Qt.RoundJoin))
     #painter.setFont(QFont('Decorative', 10))
     painter.drawImage(event.rect(), self.image)
     painter.drawPath(self.path)

  def mousePressEvent(self, event):
     self.path.moveTo(event.pos())
     self.update()

  def mouseMoveEvent(self, event):
     self.path.lineTo(event.pos())
     self.newPoint.emit(event.pos())
     self.update()

  def sizeHint(self):
     return QSize(300, 300)

当我调用它时,我使用此函数调用Drawer类中的函数

def saveFile(self):#, fileFormat):

      fileFormat="PNG"
      fileName="ar.png"

      self.draw2.saveImage(fileName,fileFormat)

1 个答案:

答案 0 :(得分:1)

它总是空的,因为你从未画过图像,你画的是QWidget的背景,在下面的代码中我创建了一个QPainter作为基础图像并在其上绘制,在paintEvent()中仅绘制图像:

import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *


class Drawer(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        self.setAttribute(Qt.WA_StaticContents)
        h = 400
        w = 400
        self.myPenWidth = 13
        self.myPenColor = Qt.black
        self.image = QImage(w, h, QImage.Format_RGB32)
        self.path = QPainterPath()
        self.clearImage()

    def setPenColor(self, newColor):
        self.myPenColor = newColor

    def setPenWidth(self, newWidth):
        self.myPenWidth = newWidth

    def clearImage(self):
        self.path = QPainterPath()
        self.image.fill(Qt.white)  ## switch it to else
        self.update()

    def saveImage(self, fileName, fileFormat):
        self.image.save(fileName, fileFormat)

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.drawImage(event.rect(), self.image, self.rect())

    def mousePressEvent(self, event):
        self.path.moveTo(event.pos())

    def mouseMoveEvent(self, event):
        self.path.lineTo(event.pos())
        p = QPainter(self.image)
        p.setPen(QPen(self.myPenColor,
                      self.myPenWidth, Qt.SolidLine, Qt.RoundCap,
                      Qt.RoundJoin))
        p.drawPath(self.path)
        p.end()
        self.update()

    def sizeHint(self):
        return QSize(300, 300)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = QWidget()
    btnSave = QPushButton("Save image")
    btnClear = QPushButton("Clear")
    drawer = Drawer()

    w.setLayout(QVBoxLayout())
    w.layout().addWidget(btnSave)
    w.layout().addWidget(btnClear)
    w.layout().addWidget(drawer)

    btnSave.clicked.connect(lambda: drawer.saveImage("image.png", "PNG"))
    btnClear.clicked.connect(drawer.clearImage)

    w.show()
    sys.exit(app.exec_())