在 Windows 上使用 Python 从网络摄像头捕获图像

Mar*_*III 1 python webcam image save capture

我现在使用的代码是:-

from VideoCapture import Device
cam = Device()
cam.saveSnapshot('image.jpg')
Run Code Online (Sandbox Code Playgroud)

使用 py 2.7 和导入的 pygame 以及 all 和 videocapture 我在 pycharm 中收到此错误:-

C:\Python27\python.exe F:/Xtraz/Orion/Key-Logger.py
Traceback (most recent call last):
  File "F:/Xtraz/Orion/Key-Logger.py", line 3, in <module>
    cam.saveSnapshot('image.jpg')
  File "C:\Python27\lib\VideoCapture.py", line 200, in saveSnapshot
    self.getImage(timestamp, boldfont, textpos).save(filename, **keywords)
  File "C:\Python27\lib\VideoCapture.py", line 138, in getImage
    im = Image.fromstring('RGB', (width, height), buffer, 'raw', 'BGR', 0, -1)
  File "C:\Users\admin\AppData\Roaming\Python\Python27\site-packages\PIL\Image.py", line 2080, in fromstring
    "Please call frombytes() instead.")
NotImplementedError: fromstring() has been removed. Please call frombytes() instead.

Process finished with exit code 1
Run Code Online (Sandbox Code Playgroud)

网络摄像头 LED 亮起,然后立即熄灭。或帮助我处理任何其他仅适用于 Windows 上的 py 2.7 和 pycharm 的代码和库!我只想保存图像,而不是显示它!

evu*_*uez 5

您可能想要降级您的 PIL 版本,似乎 VideoCapture 已经有一段时间没有更新了,并且仍然依赖于过时的 PIL 版本。

PIL 2.x 似乎有一个工作fromstring方法:https : //github.com/python-pillow/Pillow/blob/2.9.0/PIL/Image.py#L750

否则,您可以尝试将第 138 行VideoCapture.py从更改im = Image.fromstring(...)im = Image.frombytes(...); 希望这是阻止它工作的唯一原因。

解决方案 #1:降级 PIL

如果您正在使用pip,则可以使用卸载当前版本pip uninstall Pillow,然后使用pip install Pillow==2.9.0Pillow是 PIL 的一个分支,PIL 基本上已死)安装旧版本。

解决方案 #2:更新 VideoCatpure

打开文件C:\Python27\lib\VideoCapture.py并转到第 138 行。您应该有这样的内容:

im = Image.fromstring('RGB', (width, height), buffer, 'raw', 'BGR', 0, -1)
Run Code Online (Sandbox Code Playgroud)

将此行替换为:

im = Image.frombytes('RGB', (width, height), buffer, 'raw', 'BGR', 0, -1)
Run Code Online (Sandbox Code Playgroud)