Python + OpenCV - 读取图像文件名 [英] Python + OpenCV - Reading the image file name
本文介绍了Python + OpenCV - 读取图像文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码片段:
img = cv2.imread('1.jpg')
当我print img
时,我得到如下所示的结果.如何只返回 1.jpg
部分?
[[[[140 149 139][153 162 152][155 165 153]...,[ 44 20 8][ 46 22 10][ 46 22 10]][[151 160 150][156 165 155][152 162 150]...,[ 47 23 11][ 48 24 12][ 45 21 9]][[155 164 154][152 161 151][146 156 144]...,[ 47 23 11][ 49 25 13][ 49 25 13]]...,[[ 28 16 6][ 33 21 11][ 32 20 10]...,[144 131 105][150 137 111][151 138 112]][[ 33 18 9][ 34 19 10][ 34 20 8]...,[144 135 108][143 134 107][148 139 112]][[ 31 16 7][ 31 16 7][ 35 21 9]...,[145 141 112][137 133 105][143 139 111]]]
谢谢.
解决方案
我相信 cv2.imread
返回一个 numpy 数组.因此,除非您使用自定义类,否则无法存储名称.
类 MyImage:def __init__(self, img_name):self.img = cv2.imread(img_name)self.__name = img_namedef __str__(self):返回 self.__name
然后,您可以将其用作:
<预><代码>>>>x = MyImage('1.jpg')>>>字符串(x)1.jpg>>>x.img # numpy 数组I have the following code snippet:
img = cv2.imread('1.jpg')
When I print img
, I get the result shown below. How can I return the 1.jpg
part only?
[[[140 149 139]
[153 162 152]
[155 165 153]
...,
[ 44 20 8]
[ 46 22 10]
[ 46 22 10]]
[[151 160 150]
[156 165 155]
[152 162 150]
...,
[ 47 23 11]
[ 48 24 12]
[ 45 21 9]]
[[155 164 154]
[152 161 151]
[146 156 144]
...,
[ 47 23 11]
[ 49 25 13]
[ 49 25 13]]
...,
[[ 28 16 6]
[ 33 21 11]
[ 32 20 10]
...,
[144 131 105]
[150 137 111]
[151 138 112]]
[[ 33 18 9]
[ 34 19 10]
[ 34 20 8]
...,
[144 135 108]
[143 134 107]
[148 139 112]]
[[ 31 16 7]
[ 31 16 7]
[ 35 21 9]
...,
[145 141 112]
[137 133 105]
[143 139 111]]]
Thanks.
解决方案
I believe cv2.imread
returns a numpy array. So, there is no way to store the name unless you use a custom class.
class MyImage:
def __init__(self, img_name):
self.img = cv2.imread(img_name)
self.__name = img_name
def __str__(self):
return self.__name
Then, you can use this as:
>>> x = MyImage('1.jpg')
>>> str(x)
1.jpg
>>> x.img # the numpy array
这篇关于Python + OpenCV - 读取图像文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文