我打开一些图片:
image = Image.open(路径)
现在我想将他们的名字存储在一个变量中,以便我以后可以使用它们,我该怎么做?
想法是创建一个循环:
for i in range (0,20) # I have 20 images
从那里我们可以创造一个条件:
if variable_containing_image_name == 'something*.jpg'
else:
P.S。:我正在使用:
from PIL import Image
import glob, os
import numpy as np
答案 0 :(得分:0)
对周围的代码不完全清楚,但肯定会拆分路径字符串并获取最终元素? 像:
filename = path.split("/")[len(path.split("/")-1]
filenames.append(filename)
答案 1 :(得分:0)
以下代码段将从路径变量指定的目录中获取所有文件,并将它们放在图像列表中,只要它们以' .jpg'结尾。然后,您可以遍历图像列表并随意执行任何操作。
import os
from PIL import Image
path = 'the path to your image directory'
# Store the image file names in a list as long as they are jpgs
images = [f for f in os.listdir(path) if os.path.splitext(f)[-1] == '.jpg']
for image in images:
Image.open(image)
# Do whatever you need to do with the image