![](/img/trans.png)
[英]is there a way to print a different variable every iteration in a for loop?
[英]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.