Skip to content

Instantly share code, notes, and snippets.

@AllanChain
Created October 26, 2023 08:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AllanChain/123b77e3415332f63a434db0eff01644 to your computer and use it in GitHub Desktop.
Save AllanChain/123b77e3415332f63a434db0eff01644 to your computer and use it in GitHub Desktop.
Pin image on desktop after spectacle screenshot. Works on Wayland.
#!/usr/bin/python3
import sys
import subprocess
from PySide6 import QtCore, QtWidgets, QtGui
class MyWidget(QtWidgets.QLabel):
def __init__(self):
super().__init__()
self.window().setWindowFlag(QtCore.Qt.Window
| QtCore.Qt.FramelessWindowHint
| QtCore.Qt.WindowStaysOnTopHint)
self.image: QtGui.QPixmap | None = None
def load_image(self) -> None:
clipboard = QtGui.QGuiApplication.clipboard()
clipboard_image = clipboard.pixmap()
if clipboard_image.isNull():
self.close()
self.image = clipboard_image
self.dpr = self.window().devicePixelRatio()
clipboard_image.setDevicePixelRatio(self.dpr)
self.resize(int(clipboard_image.width() / self.dpr),
int(clipboard_image.height() / self.dpr))
self.setPixmap(clipboard_image)
def mouseDoubleClickEvent(self, _) -> None:
self.close()
def mouseMoveEvent(self, _):
self.window().windowHandle().startSystemMove()
def wheelEvent(self, event: QtGui.QWheelEvent) -> None:
if event.angleDelta().y() > 0:
clipboard_image = self.image.scaledToWidth(
int(self.pixmap().width() * 1.1))
elif event.angleDelta().y() < 0:
clipboard_image = self.image.scaledToWidth(
int(self.pixmap().width() / 1.1))
self.resize(int(clipboard_image.width() / self.dpr),
int(clipboard_image.height() / self.dpr))
self.setPixmap(clipboard_image)
def main():
ret = subprocess.run(["spectacle", "--background",
"--region", "--copy-image", "--nonotify"])
if ret.returncode != 0:
return
app = QtWidgets.QApplication([])
widget = MyWidget()
widget.show()
widget.window().setWindowTitle("Pimg")
QtCore.QTimer.singleShot(100, widget.load_image)
sys.exit(app.exec())
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment