[PYTHON] Drag and drop screenshots of dual screens

Task

When I set it to dual screen, both screens appear in one image with the Windows default screenshot. I want an image of only one screen.

environment

solution

--Implemented with python + OpenCV --OpenCV does not support Japanese path, so go through numpy --Allow image files to be dragged and dropped into bat -(Because it is inconvenient to type a command every time)

Implementation

screenshot-cropper.py


# -*- coding:utf8 -*-
import sys
import os
import cv2
import numpy as np

def main():
    if not (len(sys.argv) == 2):
        return
    img_path = sys.argv[1]
    if not os.path.exists(img_path):
        return
    img_array = np.fromfile(img_path, dtype=np.uint8)
    img = cv2.imdecode(img_array, cv2.IMREAD_COLOR)
    img_cropped = img[0:1080, 1080:3000]
    root, ext = os.path.splitext(img_path)
    img_cropped_path = root + '_cropped' + ext
    result, img_cropped_array = cv2.imencode(ext, img_cropped)
    img_cropped_array.tofile(img_cropped_path)

if __name__ == '__main__':
    main()

screenshot-cropper.bat


python "C:\path\to\screenshot-cropper.py" %1

Recommended Posts

Drag and drop screenshots of dual screens
Screenshots of Megalodon in selenium and Chrome.
Drag and drop local files with Selenium (Python)