简体   繁体   English

如何打印在python中for循环的每次迭代中读取的图像名称

[英]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.

相关问题 有没有办法在 for 循环中每次迭代都打印一个不同的变量? - is there a way to print a different variable every iteration in a for loop? Python - 如何通过每次“迭代”更新自身的概率来结束例程? - Python - How to end routine by probability which updates itself with every 'iteration'? 如何在Python中的for循环的每次迭代中创建一个新的数据框 - How to create a new dataframe with every iteration of for loop in Python 在 python 中使用循环抓取时,如何将每次迭代的 output 导出到 csv - How to export to csv the output of every iteration when scraping with a loop in python 如何在Python字典中的迭代中打印当前循环号? - How to print current loop number in an iteration over a dictionary in Python? 如何使 Python 在 For 循环中第 n 次迭代时打印一些内容? - How to Make Python Print something at nth Iteration within a For Loop? 如何在 Python 中打印出 for 循环的每次迭代? - How do I print out each iteration of the for loop in Python? 列表是否在 python &#39;for&#39; 循环的每次迭代中实例化? - Does list instantiates at every iteration of python 'for' loop? python中for循环的每次迭代都为变量添加后缀 - Adding suffix to variable for every iteration of for loop in python 如何打印来自while循环的每个输入值 - How to print every input value which come from a while loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM