Python枕头库screensh

2024-04-27 01:08:24 发布

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

为什么我可以使用ImageGrab.grab(300,200,400,150)而不是这个:ImageGrab.grab(bbox)

代码和错误消息:

def wl(wx,wy):
    local = win32gui.GetWindowRect(wds)
    dx = wx+local[0]
    dy = wy+local[1]
    return (dx,dy)
def wlw (x,y,z,a):
    return wl(x,y)+wl(z,a)
bbox = wlw(300,200,400,150)
pic = ImageGrab.grab(bbox)
pic.save(r'C:\Users\shang\Desktop\test.jpg')

Traceback (most recent call last):
  File "C:/Users/shang/PycharmProjects/test/First.py", line 90, in <module>
    pic.save(r'C:\Users\shang\Desktop\test.jpg')
  File "C:\Users\shang\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\Image.py", line 2088, in save
    save_handler(self, fp, filename)
  File "C:\Users\shang\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\JpegImagePlugin.py", line 779, in _save
    ImageFile._save(im, fp, [("jpeg", (0, 0) + im.size, 0, rawmode)], bufsize)
  File "C:\Users\shang\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\ImageFile.py", line 525, in _save
    e.setimage(im.im, b)
SystemError: tile cannot extend outside image

Process finished with exit code 1

Tags: inpytestsavelocallineusersfile
1条回答
网友
1楼 · 发布于 2024-04-27 01:08:24

我无法运行该程序,因为我没有不想安装的wdswin32gui。请注意,bbox是一个具有4个值的元组,而ImageGrab.grab具有4个参数,因此请尝试ImageGrab.grab(*bbox),其中元组扩展为4个变量。所以:

def wl(wx, wy):
    local = win32gui.GetWindowRect(wds)
    dx = wx + local[0]
    dy = wy + local[1]
    return (dx,dy)


def wlw (x, y, z, a):
    return wl(x, y) + wl(z, a)


bbox = wlw(300, 200, 400, 150)
pic = ImageGrab.grab(*bbox)

pic.save('C:\Users\shang\Desktop\test.jpg')

一些建议。试着遵循PEP8的指导方针,使用空格和白线来提高代码的可读性(比如逗号后面的空格和函数之间的两行)。 使名称更具描述性,什么是函数wlwlw?什么是localza?当一个函数描述这些函数是什么样子的get_local_window或者类似的东西时。 是否真的有必要使用两个函数wlwlw,难道wl不只是wlw的一部分吗

相关问题 更多 >