从图像列表中查找最大的图像尺寸

Mri*_*lla 3 python

我有一个本地保存的图像列表(路径).如何从这些中找到最大的图像?我不是指文件大小而是指尺寸.

所有图像都是常见的网络兼容格式 - JPG,GIF,PNG等.

谢谢.

dug*_*res 7

假设图像的"大小"是其面积:

from PIL import Image

def get_img_size(path):
    width, height = Image.open(path).size
    return width*height

largest = max(the_paths, key=get_img_size)
Run Code Online (Sandbox Code Playgroud)


Man*_*dan 5

使用Python Imaging Library(PIL).像这样的东西:

from PIL import Image
filenames = ['/home/you/Desktop/chstamp.jpg', '/home/you/Desktop/something.jpg']
sizes = [Image.open(f, 'r').size for f in filenames]
max(sizes)
Run Code Online (Sandbox Code Playgroud)

更新(谢谢德尔南):

将以上代码段的最后两行替换为:

max(Image.open(f, 'r').size for f in filenames)
Run Code Online (Sandbox Code Playgroud)

更新2

OP想要找到与最大尺寸对应的文件的索引.这需要一些帮助numpy.见下文:

from numpy import array
image_array = array([Image.open(f, 'r').size for f in filenames])
print image_array.argmax()
Run Code Online (Sandbox Code Playgroud)