import zbar
import Image
import cv2
# create a reader
scanner = zbar.ImageScanner()
# configure the reader
scanner.parse_config('enable')
#create video capture feed
cap = cv2.VideoCapture(0)
while(True):
ret, cv = cap.read()
cv = cv2.cvtColor(cv, cv2.COLOR_BGR2GRAY)
pil = Image.fromarray(cv)
width, height = pil.size
raw = pil.tostring()
# wrap image data
image = zbar.Image(width, height, 'Y800', raw)
# scan the image for barcodes
scanner.scan(image)
# extract results
for symbol in image:
# do something useful with results
print 'decoded', symbol.type, 'symbol', '"%s"' % symbol.data
# clean up
print "/n ...Done"
我运行此代码,但这显示此错误
Traceback (most recent call last):
File "/home/joeydash/Desktop/InterIIT-UAV-challenge/zbar/main.py", line 17, in <module>
raw = pil.tostring()
File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 695, in tostring
"Please call tobytes() instead.")
Exception: tostring() has been removed. Please call tobytes() instead.
不知道image.tostring函数意味着什么。我怎么解决这个问题?
所有信用标记杰伊的评论。您的代码将如下所示
import zbar
import Image
import cv2
# create a reader
scanner = zbar.ImageScanner()
# configure the reader
scanner.parse_config('enable')
#create video capture feed
cap = cv2.VideoCapture(0)
while(True):
ret, cv = cap.read()
cv = cv2.cvtColor(cv, cv2.COLOR_BGR2GRAY)
pil = Image.fromarray(cv)
width, height = pil.size
raw = pil.tobytes()
# wrap image data
image = zbar.Image(width, height, 'Y800', raw)
# scan the image for barcodes
scanner.scan(image)
# extract results
for symbol in image:
# do something useful with results
print 'decoded', symbol.type, 'symbol', '"%s"' % symbol.data
# clean up
print "/n ...Done"
请注意raw = pil.tostring()
行如何更改为raw = pil.tobytes()
希望这可以帮助!