admin管理员组

文章数量:1401618

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’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.

Share Improve this question asked Mar 24 at 17:54 JackJack 154 bronze badges 1
  • Please read How to Ask and minimal reproducible example. Then click edit and add your code. Your image width parameter is likely currently incorrectly set which causes the distortion. – Mark Setchell Commented Mar 25 at 1:30
Add a comment  | 

1 Answer 1

Reset to default 1

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()

本文标签: