Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
134 views
in Technique[技术] by (71.8m points)

python - Turn numpy array into a string

I have a numpy array. print(numpy_array[1]) gives an output of ['path/to/file.jpg']

I want to use cv.imread(numpy_array[1]), but when I do that I get the error:

 Can't convert object of type 'numpy.ndarray' to 'str' for 'filename'
question from:https://stackoverflow.com/questions/65845201/turn-numpy-array-into-a-string

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have a 2D list, means, numpy_array[1] is not a string but a list, and inside that list there is a string, so use numpy_array[1, 0], to get the string inside it, or remove the extra dimension using numpy.squeeze(numpy_array), then using numpy_array[1] will work.

cv.imread(numpy_array[1, 0])

or

numpy_array = numpy.squeeze(numpy_array)
cv.imread(numpy_array[1])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...