[英]How to print the name of the image which is read in every iteration of the for loop in python
I am trying to find all the grayscale images inside a folder named "train".我试图在名为“train”的文件夹中找到所有灰度图像。 For that, my code is
为此,我的代码是
for img in glob.glob("Tensorflow/workspace/images/train/*.jpg"):
image = imread(img)
if(len(image.shape) < 3):
print('gray')
# print(image)
# print(os.path.dirname(os.path.abspath(__file__)))
# print(__file__)
# print(os.path.basename('image'))
elif len(image.shape) == 3:
print('Color(RGB)')
else:
print('others')
I have tried a lot of ways to do so ( the commented part in my code), but they print the path of the python file like e:\Desktop\RealTimeFaceRecognition---Microsoft-Engage\Tensorflow\scripts\rgb_checker.py
But I want to print the image name every time it is in grayscale, for example, if image " mask43
" is found to be grayscale, then I want to print E:\Desktop\RealTimeFaceRecognition---Microsoft-Engage\Tensorflow\workspace\images\train\mask43
, which is the path of the image, not the python file inside which I am checking this.我已经尝试了很多方法来做到这一点(我的代码中的注释部分),但是它们打印了 python 文件的路径,例如
e:\Desktop\RealTimeFaceRecognition---Microsoft-Engage\Tensorflow\scripts\rgb_checker.py
但是我想在每次灰度时打印图像名称,例如,如果发现图像“ mask43
”是灰度的,那么我想打印E:\Desktop\RealTimeFaceRecognition---Microsoft-Engage\Tensorflow\workspace\images\train\mask43
,这是图像的路径,而不是我正在检查的 python 文件。
Please tell me the required code to do so.请告诉我这样做所需的代码。
You have a for loop which have a variable img
in the for loop that keeps track of which file it is.你有一个 for 循环,它在 for 循环中有一个变量
img
来跟踪它是哪个文件。 So that means you can access current file that's in the list by print(os.path.dirname(img))
这意味着您可以通过
print(os.path.dirname(img))
访问列表中的当前文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.