【问题标题】:How to read an image name with PIL如何使用 PIL 读取图像名称
【发布时间】:2017-10-17 11:12:26
【问题描述】:

我要打开一些图片:

image = Image.open(路径)

现在我想将它们的名称存储在一个变量中,以便以后使用它们,我该怎么做?

这个想法是创建一个循环:

for i in range (0,20) # I have 20 images

从那里我们可以创建一个条件:

if variable_containing_image_name == 'something*.jpg'

else:

PS:我正在使用:

from PIL import Image
import glob, os
import numpy as np

【问题讨论】:

    标签: python python-2.7 image-processing python-imaging-library


    【解决方案1】:

    下面的 sn-p 将从 path 变量指定的目录中取出所有文件,并将它们放在图像列表中,只要它们以'.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
    

    【讨论】:

      【解决方案2】:

      周围的代码不完全清楚,但肯定会拆分路径字符串并获取最终元素? 喜欢:

      filename = path.split("/")[len(path.split("/")-1]
      filenames.append(filename)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-05
        • 2015-12-17
        • 1970-01-01
        • 2013-01-05
        • 2012-04-16
        相关资源
        最近更新 更多