I’m working on a Python script to capture screenshots on macOS in a stealthy way using Core Graphics (CGWindowListCreateImage) and PIL (Pillow). However, the screenshots I capture are heavily distorted—text and elements are smeared and unreadable. I’ve tried several approaches, but I can’t get a clear image, and I need help fixing this while keeping the method stealthy.
I think this script can successfully grab screenshot. Image will not be blurry, if we save image through NSBitmapImageRep.
import Quartz
import Cocoa
def capture_screenshot():
# Get the main display's bounds
main_display_id = Quartz.CGMainDisplayID()
display_bounds = Quartz.CGDisplayBounds(main_display_id)
screenshot_image = Quartz.CGWindowListCreateImage(
display_bounds,
Quartz.kCGWindowListOptionOnScreenOnly,
Quartz.kCGNullWindowID,
Quartz.kCGWindowImageDefault
)
# Check if the image was captured successfully
if screenshot_image is None:
print("Failed to capture screenshot")
return
# Create a destination for the screenshot
destination_url = Cocoa.NSURL.fileURLWithPath_("screenshot.png")
# Create a bitmap context to draw the image
bitmap_rep = Cocoa.NSBitmapImageRep.alloc().initWithCGImage_(screenshot_image)
png_data = bitmap_rep.representationUsingType_properties_(Cocoa.NSBitmapImageFileTypePNG, None)
# Write the PNG data to file
png_data.writeToURL_atomically_(destination_url, True)
print(f"Screenshot saved as {destination_url.path()}")
if __name__ == "__main__":
capture_screenshot()