我想使用pyscreenshot

时间:2019-04-29 11:59:30

标签: python selenium-webdriver browser-automation

我使用了pyscreenshot软件包,并且在运行脚本时遇到以下错误。我正在尝试拍摄特定区域的屏幕截图。 下面是我的脚本:

import pyscreenshot as ImageGrab

im=ImageGrab.grab(bbox=(10,10,500,500))

im.save('im.png')

if __name__ == '__main__':
    pass

================================================ ================================   追溯(最近一次通话):       文件“”,第1行,位于       主文件“ C:\ Python27 \ lib \ multiprocessing \ forking.py”,第380行         准备(准备数据)       准备文件“ C:\ Python27 \ lib \ multiprocessing \ forking.py”,行509         “ parents_main ”,文件,路径名等       第9行的文件“ C:\ harsh \ CodeForAutomation \ latest_25jan2019 \ aha-gui-fvt \ pytesseract \ pytes \ test_pyscreenshot_localised.py”         im = ImageGrab.grab(bbox =(10,10,500,500))       抓取文件“ build \ bdist.win32 \ egg \ pyscreenshot__init __。py”,第67行

  File "build\bdist.win32\egg\pyscreenshot\__init__.py", line 46, in _grab

  File "build\bdist.win32\egg\pyscreenshot\procutil.py", line 31, in run_in_childprocess
  File "C:\Python27\lib\multiprocessing\process.py", line 130, in start
    self._popen = Popen(self)
  File "C:\Python27\lib\multiprocessing\forking.py", line 258, in __init__
    cmd = get_command_line() + [rhandle]
  File "C:\Python27\lib\multiprocessing\forking.py", line 358, in get_command_line
    is not going to be frozen to produce a Windows executable.''')
RuntimeError: 
            Attempt to start a new process before the current process
            has finished its bootstrapping phase.

            This probably means that you are on Windows and you have
            forgotten to use the proper idiom in the main module:

                if __name__ == '__main__':
                    freeze_support()
                    ...

            The "freeze_support()" line can be omitted if the program
            is not going to be frozen to produce a Windows executable.

2 个答案:

答案 0 :(得分:1)

TL; DR if __name__ == __main__内移动代码(无论如何,这都是最佳做法)

import pyscreenshot as ImageGrab

if __name__ == '__main__':
    im = ImageGrab.grab(bbox=(10, 10, 500, 500))
    im.save('im.png')


似乎pyscreenshot正在使用多进程和派生。

根据收到的错误消息及其pypi page上的示例,每个使用pyscreenshot的代码都必须是可腌制的。

答案 1 :(得分:-1)

我可以通过进行如下较小的更改来运行此脚本。

import pyscreenshot as ImageGrab

if __name__ == '__main__':
    im=ImageGrab.grab(bbox=(100,100,800,800))
    im.show('im.jpg')