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
- '''
- Программа выполняет создание скриншота заданной области экрана, сохраняет его в файл и копирует в буфер обмена.
- Если существует файл настроек, программа использует его для определения области захвата;
- в противном случае использует заранее заданные размеры.
- '''
- 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
- def load_coordinates_from_json():
- # Путь к JSON файлу
- json_file_path = 'settings for screenshot.json'
- if os.path.exists(json_file_path): # Проверяем существует ли файл
- with open(json_file_path, 'r') as json_file:
- coords = json.load(json_file)
- # Присваиваем значения из словаря переменным
- left = int(coords['left'])
- top = int(coords['top'])
- width = int(coords['width'])
- height = int(coords['height'])
- # Вызываем функцию скриншота с расчитанными параметрами
- screenshot(left, top, width, height)
- else:
- # Задаем размеры скриншота
- top = int(130)
- left = int(110)
- width = 1530
- height = 910
- screenshot(left, top, width, height)
- load_coordinates_from_json()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement