python - 将numpy数组转换为字符串
问题描述
我有一个 numpy 数组。
print(numpy_array[1])
给出一个输出['path/to/file.jpg']
我想使用cv.imread(numpy_array[1])
,但是当我这样做时,我得到了错误:
Can't convert object of type 'numpy.ndarray' to 'str' for 'filename'
解决方案
['path/to/file.jpg']
是一个列表。您想要的可能是'path/to/file.jpg'
,这是此列表的第一个元素:['path/to/file.jpg'][0]
.
对于您的 CV 电话,请尝试cv.imread(numpy_array[1][0])
.
或者,正如 nagyl 所建议的,您可以展平您的阵列。这是一个例子
>>> a = np.array([["path"],["file"]])
>>> a.flatten()
array(['path', 'file'], dtype='<U4')
推荐阅读
- c# - 每次我击键时都会发生 IntegerUpDown ValueChangeEvent
- azureservicebus - Azure func 无法同时监听 2 个订阅
- c# - 在选项标签中显示数据库中的项目名称
- sapui5 - SAP Fiori Elements 中的副作用实施
- ios - SwiftUI:iOS 15 中出现故障的 NavigationBar 和 TabBar
- ansible - Ansible 无法解析 kubectl patch 命令
- javascript - 使用 cookie 重新加载页面后将所选语言保留在下拉列表中
- angular - 避免 Angular 组件中的内存泄漏
- plaid - 如何监控格子里的负债
- origen-sdk - 如何在四模式或双模式下生成 v93k 模式?