【问题标题】:using im.format and im.size for a whole directory rather than a single jpeg file对整个目录而不是单个 jpeg 文件使用 im.format 和 im.size
【发布时间】:2014-03-16 11:43:16
【问题描述】:
我已经列出了某个目录中的所有 .jpg 文件,并使用 Image.open 来查找图像的大小。 PIL 有没有办法只为特定目录中的每个单独文件执行 sam?
os.chdir('randomDir')
im=Image.open("01080_creteclifs_1680x1050.jpg")
print(im.format, im.size)
整个文件夹、目录或其他有类似的方法吗?
【问题讨论】:
标签:
python
image
directory
python-imaging-library
pillow
【解决方案1】:
恐怕 PIL 只能在文件的基础上工作,所以您必须自己编写其余的代码。幸运的是,使用 Python 很容易做到这一点:
for path, dirs, files in os.walk('randomDir'):
for fpath in files:
im = Image.open(os.path.join(path, fpath))
print(im.format, im.size)