我想用pyscreensh在网页上截取一个特定区域的截图

2024-04-19 20:53:42 发布

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

我使用了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\分叉.py“,第380行,主 准备(准备数据) 文件“C:\Python27\lib\multiprocessing\分叉.py“,第509行,in prepare” “父项\u main”、文件、路径\u名称等 文件“C:\harsh\CodeForAutomation\latest\u 25jan2019\aha gui fvt\pytesseract\pytes\test\u pyscreenshot_本地化.py,第9行,在 即时消息=图像抓取。抓取(bbox=(1010500500)) 文件“生成”\bdist.win32文件\egg\pyscreenshot\uuu init\uuuuuuuuuuuuuuy.py“,第67行,在grab中

  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.

Tags: 文件thetoinpyifmainlib
2条回答

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的所有代码都必须是可pickle的。你知道吗

我能够运行这个脚本,通过做如下的小改动。你知道吗

import pyscreenshot as ImageGrab

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

相关问题 更多 >