Advertisement
egor230

Создать рамка для скриншота в linux

Apr 23rd, 2024
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. import json, pyautogui, subprocess, time, os, sys
  2. from PIL import ImageGrab
  3. from PyQt5.QtWidgets import QApplication, QMainWindow
  4. from PyQt5.QtCore import Qt, QPoint, QRect
  5. from PyQt5.QtGui import QPainter, QPen
  6. '''
  7. Эта программа позволяет выбрать область экрана, делает её скриншот,
  8. копирует в буфер обмена и сохраняет параметры выбора области экрана в JSON-файл.
  9. '''
  10. def screenshot(left, top, width, height): # print(left, top, width, height, end=' ')
  11. time.sleep(0.6)
  12. # Делаем скриншот с заданными размерами и координатами
  13. screenshot = ImageGrab.grab(bbox=(left, top, left + width, top + height))
  14.  
  15. # Сохраняем изображение во временный файл
  16. temp_file_path = "/tmp/screenshot.png"
  17. screenshot.save(temp_file_path, format="PNG")
  18.  
  19. time.sleep(0.1)
  20. # Копируем изображение в буфер обмена с помощью xclip
  21. subprocess.run(['xclip', '-selection', 'clipboard', '-t', 'image/png', '-i', temp_file_path])
  22.  
  23. time.sleep(0.1)
  24. os.remove(temp_file_path)
  25.  
  26. def calculate_coordinates(begin_point, end_point): # Рассчитываем координаты top-left угла и размеры для скриншота
  27. left = min(begin_point.x(), end_point.x())
  28. top = min(begin_point.y(), end_point.y())
  29. width = abs(end_point.x() - begin_point.x())
  30. height = abs(end_point.y() - begin_point.y())
  31.  
  32. return left, top, width, height
  33. class TransparentWindow(QMainWindow):
  34. def __init__(self):
  35. super().__init__()
  36. self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
  37. self.setAttribute(Qt.WA_TranslucentBackground, True)
  38. self.setGeometry(QApplication.primaryScreen().geometry()) # Fullscreen
  39.  
  40. self.begin = QPoint()
  41. self.end = QPoint()
  42. self.show()
  43.  
  44. def paintEvent(self, event):
  45. qp = QPainter(self)
  46. qp.setPen(QPen(Qt.red, 3))
  47. qp.setBrush(Qt.transparent)
  48. rect = QRect(self.begin, self.end)
  49. qp.drawRect(rect)
  50.  
  51. def mousePressEvent(self, event):
  52. self.begin = event.pos()
  53. self.end = self.begin
  54. self.update()
  55.  
  56. def mouseMoveEvent(self, event):
  57. self.end = event.pos()
  58. self.update()
  59.  
  60. def mouseReleaseEvent(self, event):
  61. self.end = event.pos()
  62. self.update()
  63. # Рассчитываем left, top, width и height для скриншота
  64. left, top, width, height =calculate_coordinates(self.begin, self.end)
  65.  
  66. self.close()
  67. # Создаем словарь для сохранения данных
  68. coords = { 'left': left, 'top': top,
  69. 'width': width, 'height': height
  70. }
  71.  
  72. json_file_path = 'settings for screenshot.json' # Путь к json файлу
  73.  
  74. # Записываем данные в json файл
  75. with open(json_file_path, 'w') as json_file:
  76. json.dump(coords, json_file, indent=4)
  77. # Вызываем функцию скриншота с расчитанными параметрами
  78. screenshot(left, top, width, height)
  79.  
  80. if __name__ == '__main__':
  81. app = QApplication(sys.argv)
  82. window = TransparentWindow()
  83. sys.exit(app.exec_())
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement