Search code examples
pythonpyautogui

pyautogui creating screenshot in specific area knowing 2 corners


I have coordinates for 2 corners https://prnt.sc/w2jryh (x and y coordinates for d and b points of the square). And I need to create screenshot within the area of this square, but when I am trying to do that, it is failing, either getting too much in screenshot, or too less. What may be the magic formula for that :) This is what I tried:

pyautogui.screenshot("testScr.png",region=(blackRookCornerX,whiteRookCornerY,whiteRookCornerX,blackRookCornerY))

basically taking coordinates and trying get the right screenshot. Coordinates are correct here.


Solution

  • From their docs

    There is also an optional region keyword argument, if you do not want a screenshot of the entire screen. You can pass a four-integer tuple of the left, top, width, and height of the region to capture:

    The first two numbers should be the x,y coordinates of the top left corner of where you want to take a shot, the third number is how far right/left to go (in pixels) and the fourth is how far up/down to go (in pixels).

    Try this:

    pyautogui.screenshot("testScr.png", region=(blackRookCornerX, whiteRookCornerY, 100, 100))
    

    Start with a broad number like 100 and then slowly whittle away until you have the perfect screenshot.