Python屏幕截图,仅桌面背景(MacBook Catalina)

2024-09-28 20:48:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图拍摄tkinter生成的GUI窗口的屏幕截图,但是当我拍摄屏幕截图时,我只看到桌面而不是Python窗口

我阅读了新的o/s Catalina,您必须授予终端权限,我已经这样做了,但仍然无法获取python窗口。即使我做了一个全屏拍摄,我仍然看不到Python窗口

https://github.com/BoboTiG/python-mss/issues/134

我尝试了几种不同的方法,但仍然无法通过屏幕抓取Python窗口

其他人对Catalina O/S有这些问题吗

from PIL import Image, ImageTk
from tkinter import Tk, BOTH, Canvas, BOTH, NW, W
from tkinter.ttk import Frame, Label, Style
import pyscreenshot
import io
import os
import subprocess
import sys
import mss

top_border_height = 50
bottom_border_height = 70
screen_width = 800
screen_height = 480

video_icon_640x480_x = (800-640)/2
video_icon_640x480_y = (480-480)/2
homeicon64x64_x = 8
homeicon64x64_y = 8

root = Tk('test Screen')
root.geometry("800x480")
w = Canvas(root, width=screen_width, height=screen_height)

back_ground = ImageTk.PhotoImage(Image.open("./icon/wireframe_mode_background.png"))    
w.create_image(0, 0, image=back_ground, anchor='nw')
w.video_icon_640x480 = ImageTk.PhotoImage(Image.open("./icon/wireframe_640x480.png"))
w.create_image(video_icon_640x480_x, video_icon_640x480_y, image=w.video_icon_640x480, anchor="nw")
w.home_icon_640x480 = ImageTk.PhotoImage(Image.open("./icon/wireframe_64x64.png"))
w.create_image(homeicon64x64_x,homeicon64x64_y,image=w.home_icon_640x480, anchor="nw")
w.video_icon_640x480_1 = ImageTk.PhotoImage(Image.open("./icon/wireframe_64x64.png"))
w.create_image(728,8,image=w.video_icon_640x480_1, anchor="nw")
w.video_icon_640x480_2 = ImageTk.PhotoImage(Image.open("./icon/wireframe_64x64.png"))
w.create_image(728,80,image=w.video_icon_640x480_2, anchor="nw")

w.pack()
root.mainloop()

im = pyscreenshot.grab(bbox=(10, 10, 510, 510))  # X1,Y1,X2,Y2
im.save('screenshot.png')

with mss.mss() as sct:
   filename = sct.shot(mon=-1, output='fullscreen.png')
   print(filename)

enter image description here


Tags: imageimportpngvideocreateopeniconanchor
1条回答
网友
1楼 · 发布于 2024-09-28 20:48:54

答案在于Python版本(或者可能是枕头)在MacOS上设置了所需的资源,因此在不可用时请求权限。在此之前,代码/应用程序的每个用户都必须启用Python启动器,以便自Catalina之后添加屏幕录制权限。我现在只需在我的应用程序用户手册User Manual Screenshot中添加MacOS用户的限制/问题。我在Can you request the MacOS Screen Recording permission in Python就这一点提出了一个单独的具体问题

如果这发生了变化,或者您知道从Python应用程序中请求OS权限的方法,请告知我们。上述关于回答类似问题的评论是无效的。它并没有解决自Catalina之后添加的这个非常具体的MacOS许可问题。(我没有足够的分数来评论这条评论,但我可以回答这个问题;想想看。)

相关问题 更多 >