NotImplementedError: fromstring() has been removed. Please call frombytes() instead.解決方法
from VideoCapture import Device import time, string interval = 2 cam = Device(devnum=0, showVideoWindow=0) # cam.setResolution(648, 480) cam.saveSnapshot('image.jpg', timestamp=3, boldfont=1, quality=75) i = 0 quant = interval * .1 starttime = time.time() while 1: lasttime = now = int((time.time() - starttime) / interval) print(i) cam.saveSnapshot('image.jpg', timestamp=3, boldfont=1) i += 1 while now == lasttime: now = int((time.time() - starttime) / interval) time.sleeqp(quant)
執行後出現如下錯誤:
D:\Python\python3.exe "D:/PyCharm files/face/other_try/text.py"
Traceback (most recent call last):
File "D:/PyCharm files/face/other_try/text.py", line 9, in <module>
cam.saveSnapshot('image.jpg', timestamp=3, boldfont=1, quality=75)
File "D:\Python\lib\site-packages\VideoCapture\__init__.py", line 234, in saveSnapshot
self.getImage(timestamp, boldfont, textpos).save(filename, **keywords)
File "D:\Python\lib\site-packages\VideoCapture\__init__.py", line 154, in getImage
'RGB', (width, height), buffer, 'raw', 'BGR', 0, -1)
File "D:\Python\lib\site-packages\PIL\Image.py", line 2342, in fromstring
"Please call frombytes() instead.")
NotImplementedError: fromstring() has been removed. Please call frombytes() instead.
解決:
首先找到"D:\Python\lib\site-packages\PIL\Image.py"檔案的2341行,會發現程式碼如下,用raise報出NotImplementedError錯誤,而NotImplementedError表示沒有實現的錯誤。可能由於版本更新的問題,fromstring()這個函式已經不存在了,需要把遇到的fromstring修改成frombytes。
然後按照錯誤繼續往上找,找到"D:\Python\lib\site-packages\VideoCapture\__init__.py"檔案,把153行的fromstring修改成frombytes即可。
這裡附上另外一篇部落格https://blog.csdn.net/grey_csdn/article/details/77074707,對於NotImplementedError的使用和理解有很大的幫助。