我的目标是制作一个小巧的PC / Windows程序,使我可以从字面上画在屏幕顶部,并将结果另存为具有透明背景的png。像Epic Pen或gInk之类的软件,但是我的方式。全部使用Python 3.7和PyQt5。
到目前为止,我设法获得了一个功能绘图应用程序(基本上在this tutorial之后,因为我同时在学习PyQt。我设法将草稿另存为具有透明背景的png。我可以使绘图板全屏且无边框。
现在的问题是,我找不到使整个背景透明的方法。尽管我找到了使用这些方法使窗口透明和无边界的方法:
Window = Window()
Window.setStyleSheet("background:transparent;")
Window.setAttribute(Qt.WA_TranslucentBackground)
Window.setWindowFlags(Qt.FramelessWindowHint)
Window.show()
而且它的工作原理...直到我有了绘图区域。我可以在其上绘制,它将以透明背景保存,但显示为黑色。
所以我正在寻找该解决方案。即使没有PyQt,只要我可以使程序正常运行,我也不在乎。
所以这就是我所拥有的(我向您展示了框架的窗口,以便于解释):
这是我的代码:
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QMenuBar, QMenu, QAction, QShortcut, QFileDialog
from PyQt5.QtGui import QIcon, QImage, QPainter, QPen
from PyQt5.QtCore import Qt, QPoint
class Window(QMainWindow):
def __init__(self):
super().__init__()
top = 400
left = 400
width = 800
height = 600
icon = "icons/icon.png"
self.setWindowTitle("ScreenPen drawing board")
self.setGeometry(top, left, width, height)
self.setWindowIcon(QIcon(icon))
# ---------- sets image ----------
self.image = QImage(self.size(), QImage.Format_RGBA64)
self.image.fill(Qt.transparent)
# ---------- init drawing state ----------
self.drawing = False
self.brushSize = 2
self.brushColor = Qt.red
self.lastPoint = QPoint()
# ---------- Define Menus ----------
# mainmenu
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu("File")
toolMenu = mainMenu.addMenu("Tool")
toolColor = mainMenu.addMenu("Color")
# smenu save
saveAction = QAction(QIcon("icons/save.png"), "Save", self)
saveAction.setShortcut("Ctrl+S")
fileMenu.addAction(saveAction)
saveAction.triggered.connect(self.saveFrame)
# smenu clear frame
clearFrameAction = QAction(QIcon("icons/clear.png"), "Clear Frame", self)
clearFrameAction.setShortcut("Ctrl+Del")
fileMenu.addAction(clearFrameAction)
clearFrameAction.triggered.connect(self.clearFrame)
# smenu Tool Pen
toolPenAction = QAction(QIcon("icons/toolPen.png"), "Pen", self)
# clearAction.setShortcut("Ctrl+Del")
toolMenu.addAction(toolPenAction)
# ---------- Catch Mouse Down --------
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.drawing = True
self.lastPoint = event.pos()
# ---------- Catch Mouse Move --------
def mouseMoveEvent(self, event):
if (event.buttons() & Qt.LeftButton) & self.drawing:
painter = QPainter(self.image)
painter.setPen(QPen(self.brushColor, self.brushSize, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
painter.drawLine(self.lastPoint, event.pos())
self.lastPoint = event.pos()
self.update()
# ---------- Catch Mouse Up --------
def mouseReleaseEvent(self, event):
if event.button == Qt.LeftButton:
self.drawing = False
# ---------- Paint --------
def paintEvent(self, event):
canvasPainter = QPainter(self)
canvasPainter.drawImage(self.rect(), self.image, self.image.rect())
# ---------- Save Action ----------
def saveFrame(self):
filePath, _ = QFileDialog.getSaveFileName(self, "Save Image", "", "PNG(*.png);;JPEG(*.jpg *.jpeg);; ALL Files(*.*)")
if filePath == "":
return
self.image.save(filePath)
# ---------- Clear Frame Action ----------
def clearFrame(self):
self.image.fill(Qt.white)
self.update()
if __name__ == "__main__":
app = QApplication(sys.argv)
Window = Window()
# Window style
Window.setStyleSheet("background:transparent;")
Window.setAttribute(Qt.WA_TranslucentBackground)
# Window.setWindowFlags(Qt.FramelessWindowHint)
Window.show()
app.exec()
执行此操作的一种方法(在大多数平台上都应使用)是创建整个桌面的图像,然后将其裁剪到窗口所覆盖的区域。使用QScreen.grabWindow在Qt中可以很容易地做到这一点:
def saveFrame(self):
filePath, _ = QFileDialog.getSaveFileName(self, "Save Image", "", "PNG(*.png);;JPEG(*.jpg *.jpeg);; ALL Files(*.*)")
if filePath == "":
return
screen = QApplication.desktop().windowHandle().screen()
wid = QApplication.desktop().winId()
pixmap = screen.grabWindow(wid, self.x(), self.y(), self.width(), self.height())
pixmap.save(filePath)
或者可能是:
screen = self.windowHandle().screen()
pixmap = screen.grabWindow(0, self.x(), self.y(), self.width(), self.height())
pixmap.save(filePath)
这两个都在Linux上对我有用,但是我尚未在其他平台上对其进行测试。如果还想获得窗框,请使用self.frameGeometry()
获得所需的尺寸。
我不确定是否可行。作为解决方法,您可以使用python拍摄相对区域的屏幕截图,并将其用作背景。当然,如果您移动窗口,则必须更新屏幕截图。
[在Windows上使用python截屏:Get screenshot on Windows with Python?