I am creating a small program to take screenshots. It works fine when capturing the entire screen but not when capturing a specific window, as it expands a bit more than it should
import keyboard, win32gui
from tkinter import filedialog
import pyautogui as pg
# Open file explorer for save file in a location
def save_file():
global file
filetypes = [("PNG files", ".png"), ("JPG files", ".jpg"), ("All files", "*")]
file = filedialog.asksaveasfilename(defaultextension=".png",
filetypes=filetypes,
initialdir="C:/Users/Prado/Imágenes/Screenshoots",
initialfile="my_screenshoot")
# Take a screenshot of the entire screen
def full_screenshot():
save_file()
pg.screenshot(file)
# Take a screenshot of a specific window
def window_screenshot():
save_file()
w = win32gui
window_region = w.GetWindowRect(w.FindWindow(None, w.GetWindowText(w.GetForegroundWindow()))) # Get position and size of the current window
pg.screenshot(file, region=window_region)
print(window_region)
keyboard.add_hotkey("alt + insert", window_screenshot)
keyboard.add_hotkey("ctrl + insert", full_screenshot)
keyboard.wait("esc")
I already tried taking captures from different windows and the additional pixels always vary