好吧,所以我需要在我的导师自己的话语中: - 阅读images目录中的所有图像 - 为每个图像创建直方图。您的直方图功能无法使用PIL功能Image.historgam。
到目前为止,我已经掌握了这个基本代码:
from os import listdir
from PIL import Image as PImage
def loadImages(path):
imagesList = listdir(path)
loadedImages = []
for image in imagesList:
img = PImage.open(path + image)
loadedImages.append(img)
return loadedImages
path = "/
imgs = loadImages(path)
for img in imgs:
img.show()
问题是路径= /位。我不知道如何正确地说出来,以便程序从我的桌面上读取一个名为“images”的文件(或者如果你推荐的话我可以把它放在其他地方)。
请尽快回复,直到我这样做才能完成任务。
答案 0 :(得分:0)
您应该使用import os
for filename in filelist:
full_path = os.path.join(path, filename)
来处理文件路径。
os.listdir
您还应该考虑path
还在其结果中包含目录。此外,在您定义FORMAT
的代码中可能存在错误,看起来您错过了结束语。
答案 1 :(得分:0)
使用os.path.join(path, filename)
创建文件路径:
import os
import os.path
from PIL import *
def loadImages(path):
return [PImage.open(os.path.join(path, image)) for image in os.listdir(path)]
for img in loadImages('/'):
img.show()
答案 2 :(得分:0)
这是
的解决方案我认为它涵盖了原始要求
import os
from PIL import Image as PImage
def loadImages(path):
# paths to files in directory with non-files filtered out
images = filter(os.path.isfile, (os.path.join(path, name)
for name in os.listdir(path)))
loadedImages = []
for image in images:
try:
loadedImages.append(PImage.open(image))
print("{} is an image file".format(image))
except OSError:
# exception raised when PIL decides this is not an image file
print("{} is not an image file".format(image))
return loadedImages
while True:
path = input("Input image path: ")
# expand env vars and home directory tilda
path = os.path.expanduser(os.path.expandvars(path))
# check for bad input
if os.path.isdir(path):
break
print("Not a directory. Try again.")
imgs = loadImages(path)
for img in imgs:
img.show()