I am having a problem with converting encoded image to string. I encode as below:
image = cv2.imread(imagePath)
_, buffer = cv2.imencode('.png', image)
When I try to call, buffer.toString(), I get the following error:
AttributeError: 'numpy.ndarray' object has no attribute 'toString'
There are many questions on stackoverflow related to the same functionality that I am doing(Encoding with opencv). The following are few, which has contradicting answers.
In this answer, it is implied that imencode returns a tuple, with 2 elements:
>>> img_str = cv2.imencode('.jpg', img)[1].tostring()
>>> type(img_str)
'str'
Another answer to the same question, implies that the result is one value.
Anyway, in my case, the return value is a tuple with 2 elements, where the 2nd one is an ndarray, which doesn't have toString() method.
FWIW,
$ pip show opencv-python
Name: opencv-python
Version: 3.4.3.18
$ pip show numpy
Name: numpy
Version: 1.15.4
OpenCV documentation for imencode says, the return value is a tuple with 2 elements & the 2nd one is buffer.
Python: cv2.imencode(ext, img[, params]) → retval, buf
Any suggestions are appreciative, to resolve this issue.