自Catalina以来,在Python中截取屏幕截图不再在macOS上运行



我在Catalina之前使用库pyscreenshot在Python中以编程方式制作屏幕截图。

我不知道操作系统更新是否破坏了某些东西,但是自从我从代码中截取屏幕截图时,

例如

# bbox declared in context
import pyscreenshot as ImageGrab
if os.name == 'nt':
# Windows
im = ImageGrab.grab(bbox=bbox)
else:
# macOS (I don't worry about Linux)
im = ImageGrab.grab(bbox=bbox, backend='mac_quartz', childprocess=False)
# ...
im.save(filename)

我得到了干净桌面的屏幕截图,而不是屏幕上显示的应用程序。

我也试过(更丑陋但总比没有好(

os.system("screencapture %s" % filename)

但同样的事情发生了。

有没有办法解决这种情况?

您需要转到System Preferences > Security & Privacy并在屏幕录制选项下允许终端,然后它应该允许您截取屏幕截图。

请参阅Github问题

最新更新