user393267
user393267

Reputation:

Use BBOX Option In Percentage Instead Of Absolute Pixel - PyScreenShot Python

The BBOX option on the grab() function of PyScreenShot is able to collect an area of the screen which is great.

Is possible to do the same but using absolute percentage values? The problem using pixel values is that on different monitors with different resolution, the grabbed image will be different.

So instead of saying

im = ImageGrab.grab(bbox=(100,100,500,500))

I can get always the same area, independently if the screen is 1920x1080 or any other resolution

Upvotes: 0

Views: 401

Answers (1)

Tiger-222
Tiger-222

Reputation: 7150

Is it something like that your are looking for?

import mss
# import mss.tools


with mss.mss() as sct:
    monitor = sct.monitors[1]
    left = monitor['left'] + monitor['width'] * 5 // 100  # 5% from the left
    top = monitor['top'] + monitor['height'] * 5 // 100  # 5% from the top
    right = left + 400  # 400px width
    lower = top + 400  # 400px height
    box = (left, top, right, lower)
    im = sct.grab(box)
    # mss.tools.to_png(im.rgb, im.size, 'screenshot.png')

Upvotes: 0

Related Questions