More screencapture

This commit is contained in:
AuroraWright
2025-10-18 06:42:05 +02:00
parent e7ef7b433c
commit b2964a3b83

View File

@@ -999,7 +999,13 @@ class ScreenshotThread(threading.Thread):
self.screencapturekit_queue.put(None)
return
self.screencapturekit_queue.put(image)
with objc.autorelease_pool():
width = CGImageGetWidth(image)
height = CGImageGetHeight(image)
raw_data = CGDataProviderCopyData(CGImageGetDataProvider(image))
bpr = CGImageGetBytesPerRow(image)
img = Image.frombuffer('RGBA', (width, height), bytes(raw_data), 'raw', 'BGRA', bpr, 1)
self.screencapturekit_queue.put(img)
window_list = CGWindowListCopyWindowInfo(kCGWindowListOptionIncludingWindow, window_id)
if not window_list or len(window_list) == 0:
@@ -1083,26 +1089,19 @@ class ScreenshotThread(threading.Thread):
return None
if not self.window_visible:
return None
self.window_size_changed = False
if sys.platform == 'darwin':
with objc.autorelease_pool():
if self.old_macos_screenshot_api:
cg_image = CGWindowListCreateImageFromArray(CGRectNull, [self.window_id], kCGWindowImageBoundsIgnoreFraming | kCGWindowImageNominalResolution)
else:
cg_image = self.capture_macos_window_screenshot(self.window_id)
if not cg_image:
return False
width = CGImageGetWidth(cg_image)
height = CGImageGetHeight(cg_image)
raw_data = CGDataProviderCopyData(CGImageGetDataProvider(cg_image))
bpr = CGImageGetBytesPerRow(cg_image)
current_size = (width, height)
if self.window_size != current_size:
if self.window_size:
self.window_size_changed = True
self.window_size = current_size
img = Image.frombuffer('RGBA', (width, height), raw_data, 'raw', 'BGRA', bpr, 1)
img = Image.frombuffer('RGBA', (width, height), bytes(raw_data), 'raw', 'BGRA', bpr, 1)
else:
img = self.capture_macos_window_screenshot(self.window_id)
if not img:
return False
else:
try:
coord_left, coord_top, right, bottom = win32gui.GetWindowRect(self.window_handle)
@@ -1111,32 +1110,28 @@ class ScreenshotThread(threading.Thread):
current_size = (coord_width, coord_height)
if self.window_size != current_size:
if self.window_size:
window_size_changed = True
self.reset_windows_window()
self.cleanup_window_screen_capture()
hwnd_dc = win32gui.GetWindowDC(self.window_handle)
self.windows_window_mfc_dc = win32ui.CreateDCFromHandle(hwnd_dc)
self.windows_window_save_dc = self.windows_window_mfc_dc.CreateCompatibleDC()
self.windows_window_save_bitmap = win32ui.CreateBitmap()
self.windows_window_save_bitmap.CreateCompatibleBitmap(self.windows_window_mfc_dc, coord_width, coord_height)
self.windows_window_save_dc.SelectObject(self.windows_window_save_bitmap)
self.window_size = current_size
win32gui.ReleaseDC(self.window_handle, hwnd_dc)
result = ctypes.windll.user32.PrintWindow(self.window_handle, self.windows_window_save_dc.GetSafeHdc(), 2)
bmpinfo = self.windows_window_save_bitmap.GetInfo()
bmpstr = self.windows_window_save_bitmap.GetBitmapBits(True)
img = Image.frombuffer('RGB', (bmpinfo['bmWidth'], bmpinfo['bmHeight']), bmpstr, 'raw', 'BGRX', 0, 1)
return img
except pywintypes.error:
return None
return False
window_size_changed = False
if self.window_size != img.size:
if self.window_size:
window_size_changed = True
self.window_size = img.size
if self.window_area_coordinates:
if self.window_size_changed:
if window_size_changed:
self.window_area_coordinates = None
logger.warning('Window size changed, discarding area selection')
else: