Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json, pyautogui, subprocess, time, os, sys
- from PIL import ImageGrab
- from PyQt5.QtWidgets import QApplication, QMainWindow
- from PyQt5.QtCore import Qt, QPoint, QRect
- from PyQt5.QtGui import QPainter, QPen
- '''
- Эта программа позволяет выбрать область экрана, делает её скриншот,
- копирует в буфер обмена и сохраняет параметры выбора области экрана в JSON-файл.
- '''
- def screenshot(left, top, width, height): # print(left, top, width, height, end=' ')
- time.sleep(0.6)
- # Делаем скриншот с заданными размерами и координатами
- screenshot = ImageGrab.grab(bbox=(left, top, left + width, top + height))
- # Сохраняем изображение во временный файл
- temp_file_path = "/tmp/screenshot.png"
- screenshot.save(temp_file_path, format="PNG")
- time.sleep(0.1)
- # Копируем изображение в буфер обмена с помощью xclip
- subprocess.run(['xclip', '-selection', 'clipboard', '-t', 'image/png', '-i', temp_file_path])
- time.sleep(0.1)
- os.remove(temp_file_path)
- def calculate_coordinates(begin_point, end_point): # Рассчитываем координаты top-left угла и размеры для скриншота
- left = min(begin_point.x(), end_point.x())
- top = min(begin_point.y(), end_point.y())
- width = abs(end_point.x() - begin_point.x())
- height = abs(end_point.y() - begin_point.y())
- return left, top, width, height
- class TransparentWindow(QMainWindow):
- def __init__(self):
- super().__init__()
- self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
- self.setAttribute(Qt.WA_TranslucentBackground, True)
- self.setGeometry(QApplication.primaryScreen().geometry()) # Fullscreen
- self.begin = QPoint()
- self.end = QPoint()
- self.show()
- def paintEvent(self, event):
- qp = QPainter(self)
- qp.setPen(QPen(Qt.red, 3))
- qp.setBrush(Qt.transparent)
- rect = QRect(self.begin, self.end)
- qp.drawRect(rect)
- def mousePressEvent(self, event):
- self.begin = event.pos()
- self.end = self.begin
- self.update()
- def mouseMoveEvent(self, event):
- self.end = event.pos()
- self.update()
- def mouseReleaseEvent(self, event):
- self.end = event.pos()
- self.update()
- # Рассчитываем left, top, width и height для скриншота
- left, top, width, height =calculate_coordinates(self.begin, self.end)
- self.close()
- # Создаем словарь для сохранения данных
- coords = { 'left': left, 'top': top,
- 'width': width, 'height': height
- }
- json_file_path = 'settings for screenshot.json' # Путь к json файлу
- # Записываем данные в json файл
- with open(json_file_path, 'w') as json_file:
- json.dump(coords, json_file, indent=4)
- # Вызываем функцию скриншота с расчитанными параметрами
- screenshot(left, top, width, height)
- if __name__ == '__main__':
- app = QApplication(sys.argv)
- window = TransparentWindow()
- sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement