我正在尝试使用数组列表从我的本地目录中检索图像,但我从列表中获取了所有图像,但我不知道如何逐个显示列表中的图像。 如果有人知道,请帮助我。
my.py:
import os, os.path
imgs = []
# print(imgs)
path = "C:/Users/admin/PycharmProjects/my_module/static/files"
valid_images = [".jpg",".gif",".png",".tga"]
print(valid_images)
for f in os.listdir(path):
ext = os.path.splitext(f)[1]
os.listdir(path).append(f)
# Image.show(f)
答案 0 :(得分:1)
根据您的环境和可视化库,有多种选择。检查这个好answer
答案 1 :(得分:1)
您需要执行from PIL import Image
,然后才能执行Image.open(path + "/" + f).show()
。试试这个:
import os, os.path
from PIL import Image
imgs = []
path = "path/to/directory"
valid_images = [".jpg", ".gif", ".png", ".tga"]
print(valid_images)
for f in os.listdir(path):
ext = os.path.splitext(f)[1]
if ext in valid_images:
imgs.append(path + "/" + f)
img = Image.open(path + "/" + f)
img.show()
print(imgs)
答案 2 :(得分:1)
我试过了,我得到了预期的输出。 吡啶:
import os
from PIL import Image
img=[]
path="C:/Users/admin/PycharmProjects/my_module/static/files/"
for f in os.listdir(path):
ext = os.path.splitext(f)[1]
os.listdir(path).append(f)
image = Image.open(f)
image.show()