0

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.

  1. https://stackoverflow.com/a/25592959/7621143

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'
  1. https://stackoverflow.com/a/17970817/7621143

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.

1 Answer 1

2

The error clearly says:

AttributeError: 'numpy.ndarray' object has no attribute 'toString'

which means that you have mis-spelled the method. Replace toString with tostring to get the desired results.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.