我使用了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 行,在主 准备(preparation_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 "buildbdist.win32eggpyscreenshot__init__.py", line 46, in _grab
File "buildbdist.win32eggpyscreenshotprocutil.py", line 31, in run_in_childprocess
File "C:Python27libmultiprocessingprocess.py", line 130, in start
self._popen = Popen(self)
File "C:Python27libmultiprocessingforking.py", line 258, in __init__
cmd = get_command_line() + [rhandle]
File "C:Python27libmultiprocessingforking.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.
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 页面上的示例,使用 pyscreenshot 的每个代码都必须是可挑剔的。
我能够通过进行如下小的更改来运行此脚本。
import pyscreenshot as ImageGrab
if __name__ == '__main__':
im=ImageGrab.grab(bbox=(100,100,800,800))
im.show('im.jpg')