我想使用pyscreenshot在网页上截取特定区域的屏幕截图

问题描述 投票:0回答:2

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

import pyscreenshot as ImageGrab

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

im.save('im.png')

if __name__ == '__main__':
    pass

================================================== ============================== Traceback(最近一次调用最后一次):文件“C”中的文件“”,第1行\ Python27 \ lib \ multiprocessing \ forking.py“,第380行,在main prepare(prepare_data)文件”C:\ Python27 \ lib \ multiprocessing \ forking.py“,第509行,在准备'parents_main',文件,path_name,等文件“C:\ harsh \ CodeForAutomation \ latest_25jan2019 \ aha-gui-fvt \ pytesseract \ pytes \ test_pyscreenshot_localised.py”,第9行,在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.
python selenium-webdriver browser-automation
2个回答
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的每个代码都必须是pickleable。


-1
投票

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

import pyscreenshot as ImageGrab

if __name__ == '__main__':
    im=ImageGrab.grab(bbox=(100,100,800,800))
    im.show('im.jpg')
© www.soinside.com 2019 - 2024. All rights reserved.