繁体   English   中英

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

[英]Capturing image from Webcam Using Python on Windows

我现在使用的代码是:-

from VideoCapture import Device
cam = Device()
cam.saveSnapshot('image.jpg')

使用py 2.7和导入的pygame和所有视频捕获,我在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

网络摄像头LED指示灯点亮,然后立即关闭。 或仅在Windows上与py 2.7和pycharm一起使用的其他代码和库帮助我! 我只想保存图像,而不显示它!

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

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

否则,您可以尝试将VideoCapture.py的行138从im = Image.fromstring(...)更改为im = Image.frombytes(...) 希望这是阻止它运行的唯一方法。

解决方案1:降级PIL

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

解决方案2:更新VideoCatpure

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

im = Image.fromstring('RGB', (width, height), buffer, 'raw', 'BGR', 0, -1)

将此行替换为:

im = Image.frombytes('RGB', (width, height), buffer, 'raw', 'BGR', 0, -1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM