如何使用 PIL 读取图像名称
How to read an image name with PIL
我要打开一些图片:
图片=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
周围的代码不是很清楚,但肯定会拆分路径字符串并获取最后一个元素?
喜欢:
filename = path.split("/")[len(path.split("/")-1]
filenames.append(filename)
以下代码段将从路径变量指定的目录中获取所有文件,并将它们放入图像列表中,只要它们以“.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
我要打开一些图片:
图片=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
周围的代码不是很清楚,但肯定会拆分路径字符串并获取最后一个元素? 喜欢:
filename = path.split("/")[len(path.split("/")-1]
filenames.append(filename)
以下代码段将从路径变量指定的目录中获取所有文件,并将它们放入图像列表中,只要它们以“.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