为什么我尝试在 Mac 上截屏时只能得到桌面壁纸?
Why do I only get the Desktop wallpaper when trying to take a screenshot on a Mac?
我正在尝试使用 python 截取屏幕截图。但是我尝试的每个选项似乎都只 return 桌面墙纸而不是顶部的程序。
这里是 运行 我尝试过的以及我是如何做的。
首先,我使用 autopy 尝试使用 autopy.color.hex_to_rgb(autopy.screen.get_color(x, y))
从屏幕获取像素。但它只是告诉我桌面背景的像素。
然后我尝试 PIL(Pillow) 使用此代码:
from PIL import ImageGrab
im = ImageGrab.grab()
im.save('screenshot.png')
这仅 return 编辑了桌面墙纸。
最后,我尝试使用我在 thread 上找到的这个脚本。
import Quartz
import LaunchServices
from Cocoa import NSURL
import Quartz.CoreGraphics as CG
def screenshot(path, region = None):
"""region should be a CGRect, something like:
>>> import Quartz.CoreGraphics as CG
>>> region = CG.CGRectMake(0, 0, 100, 100)
>>> sp = ScreenPixel()
>>> sp.capture(region=region)
The default region is CG.CGRectInfinite (captures the full screen)
"""
if region is None:
region = CG.CGRectInfinite
# Create screenshot as CGImage
image = CG.CGWindowListCreateImage(
region,
CG.kCGWindowListOptionOnScreenOnly,
CG.kCGNullWindowID,
CG.kCGWindowImageDefault)
dpi = 72 # FIXME: Should query this from somewhere, e.g for retina displays
url = NSURL.fileURLWithPath_(path)
dest = Quartz.CGImageDestinationCreateWithURL(
url,
LaunchServices.kUTTypePNG, # file type
1, # 1 image in file
None
)
properties = {
Quartz.kCGImagePropertyDPIWidth: dpi,
Quartz.kCGImagePropertyDPIHeight: dpi,
}
# Add the image to the destination, characterizing the image with
# the properties dictionary.
Quartz.CGImageDestinationAddImage(dest, image, properties)
# When all the images (only 1 in this example) are added to the destination,
# finalize the CGImageDestination object.
Quartz.CGImageDestinationFinalize(dest)
if __name__ == '__main__':
# Capture full screen
screenshot("/tmp/testscreenshot_full.png")
# Capture region (100x100 box from top-left)
region = CG.CGRectMake(0, 0, 100, 100)
screenshot("/tmp/testscreenshot_partial.png", region=region)
又是return我的桌面墙纸。
为什么要这样做?我怎样才能像按 'cmd + shift + 3'.
一样获取屏幕截图
这是一项隐私功能,macOS 可防止任意应用查看其他应用的内容windows。要获得许可,您的应用需要 access to the screen recording permission in macOS preferences.
因为这是一个 Python 脚本,我认为你是 运行 脚本的应用程序需要获得许可,所以终端或任何 IDE 你使用。
我正在尝试使用 python 截取屏幕截图。但是我尝试的每个选项似乎都只 return 桌面墙纸而不是顶部的程序。
这里是 运行 我尝试过的以及我是如何做的。
首先,我使用 autopy 尝试使用 autopy.color.hex_to_rgb(autopy.screen.get_color(x, y))
从屏幕获取像素。但它只是告诉我桌面背景的像素。
然后我尝试 PIL(Pillow) 使用此代码:
from PIL import ImageGrab
im = ImageGrab.grab()
im.save('screenshot.png')
这仅 return 编辑了桌面墙纸。
最后,我尝试使用我在 thread 上找到的这个脚本。
import Quartz
import LaunchServices
from Cocoa import NSURL
import Quartz.CoreGraphics as CG
def screenshot(path, region = None):
"""region should be a CGRect, something like:
>>> import Quartz.CoreGraphics as CG
>>> region = CG.CGRectMake(0, 0, 100, 100)
>>> sp = ScreenPixel()
>>> sp.capture(region=region)
The default region is CG.CGRectInfinite (captures the full screen)
"""
if region is None:
region = CG.CGRectInfinite
# Create screenshot as CGImage
image = CG.CGWindowListCreateImage(
region,
CG.kCGWindowListOptionOnScreenOnly,
CG.kCGNullWindowID,
CG.kCGWindowImageDefault)
dpi = 72 # FIXME: Should query this from somewhere, e.g for retina displays
url = NSURL.fileURLWithPath_(path)
dest = Quartz.CGImageDestinationCreateWithURL(
url,
LaunchServices.kUTTypePNG, # file type
1, # 1 image in file
None
)
properties = {
Quartz.kCGImagePropertyDPIWidth: dpi,
Quartz.kCGImagePropertyDPIHeight: dpi,
}
# Add the image to the destination, characterizing the image with
# the properties dictionary.
Quartz.CGImageDestinationAddImage(dest, image, properties)
# When all the images (only 1 in this example) are added to the destination,
# finalize the CGImageDestination object.
Quartz.CGImageDestinationFinalize(dest)
if __name__ == '__main__':
# Capture full screen
screenshot("/tmp/testscreenshot_full.png")
# Capture region (100x100 box from top-left)
region = CG.CGRectMake(0, 0, 100, 100)
screenshot("/tmp/testscreenshot_partial.png", region=region)
又是return我的桌面墙纸。
为什么要这样做?我怎样才能像按 'cmd + shift + 3'.
一样获取屏幕截图这是一项隐私功能,macOS 可防止任意应用查看其他应用的内容windows。要获得许可,您的应用需要 access to the screen recording permission in macOS preferences.
因为这是一个 Python 脚本,我认为你是 运行 脚本的应用程序需要获得许可,所以终端或任何 IDE 你使用。