簡體   English   中英

如何打印在python中for循環的每次迭代中讀取的圖像名稱

[英]How to print the name of the image which is read in every iteration of the for loop in python

我試圖在名為“train”的文件夾中找到所有灰度圖像。 為此,我的代碼是

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')

我已經嘗試了很多方法來做到這一點(我的代碼中的注釋部分),但是它們打印了 python 文件的路徑,例如e:\Desktop\RealTimeFaceRecognition---Microsoft-Engage\Tensorflow\scripts\rgb_checker.py但是我想在每次灰度時打印圖像名稱,例如,如果發現圖像“ mask43 ”是灰度的,那么我想打印E:\Desktop\RealTimeFaceRecognition---Microsoft-Engage\Tensorflow\workspace\images\train\mask43 ,這是圖像的路徑,而不是我正在檢查的 python 文件。

請告訴我這樣做所需的代碼。

你有一個 for 循環,它在 for 循環中有一個變量img來跟蹤它是哪個文件。 這意味着您可以通過print(os.path.dirname(img))訪問列表中的當前文件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM