import win32gui
import win32ui
from ctypes import windll
from PIL import Image
hwnd = win32gui.FindWindow (None, 'New Text Document(4).txt - Notepad')
# # Change the line below depending on whether you want the whole window
# # or just the client area.
#left, top, right, bot = win32gui.GetClientRect(hwnd)
left, top, right, bot = win32gui.GetWindowRect(hwnd)
w = right - left
h = bot - top
hwndDC = win32gui.GetWindowDC(hwnd)
mfcDC = win32ui.CreateDCFromHandle(hwndDC)
saveDC = mfcDC.CreateCompatibleDC()
saveBitMap = win32ui.CreateBitmap()
saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)
saveDC.SelectObject(saveBitMap)
# # Change the line below depending on whether you want the whole window
# # or just the client area.
result = windll.user32.PrintWindow(hwnd, saveDC.GetSafeHdc(), 1)
result = windll.user32.PrintWindow(hwnd, saveDC.GetSafeHdc(), 0)
print(result)
bmpinfo = saveBitMap.GetInfo()
bmpstr = saveBitMap.GetBitmapBits(True)
im = Image.frombuffer('RGB', (bmpinfo['bmWidth'], bmpinfo['bmHeight']) ,bmpstr, 'raw', 'BGRX', 0, 1)
win32gui.DeleteObject(saveBitMap.GetHandle())
saveDC.DeleteDC()
mfcDC.DeleteDC()
win32gui.ReleaseDC(hwnd, hwndDC)
if result == 1:
#PrintWindow Succeeded
im.save("2.png")
I want to capture only a certain range from these codes
w = right - left
h = bot - top
I think I'm changing it here, so I'm trying to add and subtract numbers, but everything is captured based on the top left. (right + 500) -left captures a side black screen. Isn't it this part?
python
It would be more convenient to deal with the pillow image.
if result == 1:
im = im.crop(5, 5, 55, 55)
im.save("2.png")
Do it like this.
https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.crop
© 2024 OneMinuteCode. All rights reserved.