首页 > 其他分享 >Mac执行pyautogui.screenshot()时报错

Mac执行pyautogui.screenshot()时报错

时间:2023-09-12 12:26:08浏览次数:46  
标签:pyautogui -% screenshot region tmpFilename Mac im __ Pillow

报错信息

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[3], line 1
----> 1 pyautogui.screenshot()

File ~/anaconda3/lib/python3.11/site-packages/pyscreeze/__init__.py:527, in _screenshot_osx(imageFilename, region)
    523 """
    524 TODO
    525 """
    526 # TODO - use tmp name for this file.
--> 527 if tuple(PIL__version__) < (6, 2, 1):
    530     # Use the screencapture program if Pillow is older than 6.2.1, which
    531     # is when Pillow supported ImageGrab.grab() on macOS. (It may have
    532     # supported it earlier than 6.2.1, but I haven't tested it.)
    533     if imageFilename is None:
    534         tmpFilename = 'screenshot%s.png' % (datetime.datetime.now().strftime('%Y-%m%d_%H-%M-%S-%f'))

TypeError: '<' not supported between instances of 'str' and 'int'

可见指向 527 行的代码,对比2个变量时,出现类型错误,这是pyautogui的bug,我们只能自己来修复该错误。该语句是对比版本号,当PIL的版本小于 6.2.1时,才进入if语句。
PIL即Pillow库

通过 pip show Pillow 查看已安装的Pillow版本

pip show Pillow
Name: Pillow
Version: 9.4.0
Summary: Python Imaging Library (Fork)
Home-page: https://python-pillow.org
Author: Alex Clark (PIL Fork Author)
Author-email: [email protected]
License: HPND

可见在我的系统里,并不会进入if语句。
从报错信息可见文件路径为~/anaconda3/lib/python3.11/site-packages/pyscreeze/__init__.py,打开该文件,这里我们直接把 527行的代码改成 if False: 即可。

最终代码如下:

def _screenshot_osx(imageFilename=None, region=None):
    """
    TODO
    """
    # TODO - use tmp name for this file.
    # if tuple(PIL__version__) < (6, 2, 1):
    # 这里报错了。并且我使用的是9.4.0,并不会进入判断语句
    if False:            
        # Use the screencapture program if Pillow is older than 6.2.1, which
        # is when Pillow supported ImageGrab.grab() on macOS. (It may have
        # supported it earlier than 6.2.1, but I haven't tested it.)
        if imageFilename is None:
            tmpFilename = 'screenshot%s.png' % (datetime.datetime.now().strftime('%Y-%m%d_%H-%M-%S-%f'))
        else:
            tmpFilename = imageFilename
        subprocess.call(['screencapture', '-x', tmpFilename])
        im = Image.open(tmpFilename)

        if region is not None:
            assert len(region) == 4, 'region argument must be a tuple of four ints'
            region = [int(x) for x in region]
            im = im.crop((region[0], region[1], region[2] + region[0], region[3] + region[1]))
            os.unlink(tmpFilename)  # delete image of entire screen to save cropped version
            im.save(tmpFilename)
        else:
            # force loading before unlinking, Image.open() is lazy
            im.load()

        if imageFilename is None:
            os.unlink(tmpFilename)
    else:
        # Use ImageGrab.grab() to get the screenshot if Pillow version 6.3.2 or later is installed.
        im = ImageGrab.grab()
    return im

保存并重启虚拟环境。

标签:pyautogui,-%,screenshot,region,tmpFilename,Mac,im,__,Pillow
From: https://www.cnblogs.com/ZJT7098/p/17695823.html

相关文章

  • 记录Mac下vscode无法附加到本地docker容器的问题
    我想用vscode直接附加到本地docker容器,在里面编辑开发,传统的办法要在容器内设置ssh服务器,通过vscode的remote插件先进入到容器里面,再编辑开发,可是我看似乎vscode做了优化,本地容器可以免去ssh服务,通过devcontainers插件和docker插件的AttachtorunningDockercontainer、在新窗......
  • mac的m芯片上跑cuda程序
    config里parser.add_argument('--device',type=str,default='mps')main里device=torch.device(cfg['device'])train里x_batch=x_batch.astype('float32')y_batch=y_batch.astype('float32')aux_batch=a......
  • Python - 桌面自动化(PyAutoGUI)
    一、安装windows:pipinstallpyautogui-ihttps://pypi.tuna.tsinghua.edu.cn/simplemac:pipinstallpyobjc-corepipinstallpyobjcpipinstallpyautoguilinux:sudoapt-getinstallscrotpython3-tkpython3-devpipinstallpython3-xlibpipinstallpyautog......
  • 电脑mac地址查询方法?
    使用命令提示符(Windows系统自带)在Windows系统中,可以通过命令提示符来查询电脑的MAC地址。首先,按下Win+R键打开运行窗口,输入"cmd"并按下回车键打开命令提示符。在命令提示符窗口中,输入"ipconfig/all"并按下回车键。系统会列出各个网络适配器的详细信息,包括MAC地址。查找相应的网......
  • mac安装nvm
    系统:macoscatalina版本:10.15.7一.安装nvm打开终端执行这个命令安装的版本是v0.39.1curl-o-[https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh](https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh)|bash二.查看版本nvm--version三.......
  • mac开热点共享wifi
    以苹果MacBookPro,macOSMonterey系统为例,开启热点的方法如下: 打开共享打开系统偏好设置,点击里面的‘共享’。选择输出口在共享设置页面,先点击选中左边的‘互联网共享’,要注意的是,如果mac当前是通过wifi连接的,则是不能开通wifi热点的,因为不能用同一个端口既做输入口又做输出口......
  • 2023最新总结,Mac下使用Homebrew完全指南!
    2023最新总结,Mac下使用Homebrew完全指南!滚石前端成长之路  45人赞同了该文章1.介绍Homebrew是一款包管理工具,目前支持macOS和Linux系统。主要有四个部分组成:brew、homebrew-core、homebrew-cask、homebrew-bottles。 2.安装2.1执行安装脚本执行......
  • 原生input type="file"在mac上的小坑
    先上一张图://添加上传按钮$("body").append('<input title="" type="file" accept=".slx,.xlsx;" id="fileinp" style="display:none;">');今日开发功能时用了原生input去完成一个上传功能,由于只允许上传邮件方便后续解析,所以加了accept的限制......
  • 重磅!python自动化办公,终于支持 Mac下载了
    大家好,这里是程序员晚枫,小红薯/小破站也叫这个名。给小白的《50讲Python自动化办公》,课程一直在更新中,昨晚12点多,有朋友在课程群里问能不能支持Mac?今天给大家分享一个好消息:python-office终于支持mac下载了。下载命令先给大家说一下下载命令,然后再说注意事项。不论你的电脑上......
  • Machine learning note(1)
    注:本笔记不给出完整解释正规方程设\(z=\theta^{T}x\)设损失函数为\(J(\theta)\),求令\(\frac{\partialJ}{\partial\theta}=0\)的\(\theta\)由此得出最优的\(\theta\)牛顿迭代回顾一下梯度下降:\(\theta'=\theta-\alpha*\frac{\partialJ}{\partial\theta}\)另一种方法是牛......