【问题标题】:How to load set of images in a directory to a arraylist in python [closed]如何将目录中的一组图像加载到python中的arraylist [关闭]
【发布时间】:2016-06-04 08:22:35
【问题描述】:

我是 python 新手,我想将一组图像从一个目录加载到一个数组列表中。我找到了一个访问目录并打印所有图像路径的代码,但我想将所有图像加载到数组列表并使用for 循环打开图像。

这是我正在使用的代码:

import os
from glob import glob
try:
    # PIL
    import Image
except ImportError:
    # Pillow
    from PIL import Image

def process_image(img_path):
    print "Processing image: %s" % img_path
    # Open the image
    img = Image.open(img_path)

    # Do your processing here
    print img.info

    # Not strictly necessary, but let's be explicit:
    # Close the image
    del img

images_dir = "/home/user/images"

if __name__ == "__main__":
    # List all JPEG files in your directory
    images_list = glob(os.path.join(images_dir, "*.jpg"))

    for img_filename in images_list:
        img_path = os.path.join(images_dir, img_filename)
        process_image(img_path)

【问题讨论】:

  • 这段代码就是这样做的! images_list 是一个文件名列表,您可以在 for 循环中打开每个文件名并对其进行处理。 这里什么对你来说还不够?事实上,这个问题仍然不清楚。由于我们不是免费的代码编写服务,您最好的选择是通过快速 Python 教程,看看您是否可以帮助自己。

标签: python arraylist


【解决方案1】:

您必须使用PIL 来处理图像并使用glob 来查找您想要的文件。示例代码:

from PIL import Image
import glob
images = []
for filename in glob.glob('./images/*.gif'): # i.e. gif images
    im=Image.open(filename)
    images.append(im)

之后你想对图像做什么?

【讨论】:

  • 因为我使用这种方法来识别加载到数组中的图像之间的差异。还有比这更好的方法吗
猜你喜欢
  • 2012-09-04
  • 1970-01-01
  • 2012-03-31
  • 1970-01-01
  • 2015-02-12
  • 1970-01-01
  • 2016-03-18
  • 1970-01-01
  • 2016-05-28
相关资源
最近更新 更多