【发布时间】: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 教程,看看您是否可以帮助自己。