toxygen/toxygen/av/screen_sharing.py

24 lines
766 B
Python
Raw Normal View History

2024-02-13 21:00:45 +00:00
# -*- mode: python; indent-tabs-mode: nil; py-indent-offset: 4; coding: utf-8 -*-
2022-09-27 12:38:39 +00:00
2024-02-13 21:00:45 +00:00
from qtpy import QtWidgets
2022-09-27 12:38:39 +00:00
class DesktopGrabber:
def __init__(self, x, y, width, height):
self._x = x
self._y = y
self._width = width
self._height = height
self._width -= width % 4
self._height -= height % 4
self._screen = QtWidgets.QApplication.primaryScreen()
2024-02-13 21:00:45 +00:00
def read(self) -> tuple:
2022-09-27 12:38:39 +00:00
pixmap = self._screen.grabWindow(0, self._x, self._y, self._width, self._height)
image = pixmap.toImage()
s = image.bits().asstring(self._width * self._height * 4)
2022-09-27 13:51:50 +00:00
import numpy as np
2022-09-27 12:38:39 +00:00
arr = np.fromstring(s, dtype=np.uint8).reshape((self._height, self._width, 4))
return True, arr