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
1 Answer
Reset to default 1I 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()
本文标签:
版权声明:本文标题:How to Capture a Screenshot on macOS Using Core Graphics (CGWindowListCreateImage) with Python and PIL Without Distortion While 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744235652a2596541.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论