Advertisement
egor230

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

Apr 23rd, 2024 (edited)
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. import json, pyautogui, subprocess, time, os, sys
  2. from PIL import ImageGrab
  3. '''
  4. Программа выполняет создание скриншота заданной области экрана, сохраняет его в файл и копирует в буфер обмена.
  5. Если существует файл настроек, программа использует его для определения области захвата;
  6. в противном случае использует заранее заданные размеры.
  7. '''
  8. def screenshot(left, top, width, height):
  9. # print(left, top, width, height, end=' ')
  10. time.sleep(0.6)
  11. # Делаем скриншот с заданными размерами и координатами
  12. screenshot = ImageGrab.grab(bbox=(left, top, left + width, top + height))
  13.  
  14. # Сохраняем изображение во временный файл
  15. temp_file_path = "/tmp/screenshot.png"
  16. screenshot.save(temp_file_path, format="PNG")
  17.  
  18. time.sleep(0.1)
  19. # Копируем изображение в буфер обмена с помощью xclip
  20. subprocess.run(['xclip', '-selection', 'clipboard', '-t', 'image/png', '-i', temp_file_path])
  21.  
  22. time.sleep(0.1)
  23. os.remove(temp_file_path)
  24.  
  25. def calculate_coordinates(begin_point, end_point):
  26. # Рассчитываем координаты 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. return left, top, width, height
  32.  
  33. def load_coordinates_from_json():
  34. # Путь к JSON файлу
  35. json_file_path = 'settings for screenshot.json'
  36.  
  37. if os.path.exists(json_file_path): # Проверяем существует ли файл
  38. with open(json_file_path, 'r') as json_file:
  39. coords = json.load(json_file)
  40.  
  41. # Присваиваем значения из словаря переменным
  42. left = int(coords['left'])
  43. top = int(coords['top'])
  44. width = int(coords['width'])
  45. height = int(coords['height'])
  46. # Вызываем функцию скриншота с расчитанными параметрами
  47. screenshot(left, top, width, height)
  48. else:
  49. # Задаем размеры скриншота
  50. top = int(130)
  51. left = int(110)
  52. width = 1530
  53. height = 910
  54. screenshot(left, top, width, height)
  55.  
  56. load_coordinates_from_json()
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement