简体   繁体   中英

Capturing image from Webcam Using Python on Windows

the code i am using now is :-

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

using py 2.7 and imported pygame and all and videocapture i am getting this error in 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

the webcam LED lights onn , and then switches off immediately . or help me with any other code and library that works well with py 2.7 and pycharm on windows only ! and i just want to save the image , not display it !

You might want to downgrade you version of PIL, it seems like VideoCapture hasn't been updated for a while and is still relying on outdated versions of PIL.

PIL 2.x seems to have a working fromstring method: https://github.com/python-pillow/Pillow/blob/2.9.0/PIL/Image.py#L750

Otherwise you can try to change the line 138 in VideoCapture.py from im = Image.fromstring(...) to im = Image.frombytes(...) ; hopefully it's the only thing that prevents it from working.

Solution #1: Downgrade PIL

If you're using pip , you can just uninstall you current version using pip uninstall Pillow and then install an older one using pip install Pillow==2.9.0 ( Pillow is a fork of PIL, PIL being basically dead).

Solution #2: Update VideoCatpure

Open the file C:\\Python27\\lib\\VideoCapture.py and go to line 138. You should have something like that:

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

Replace this line with this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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