开发者

Find the largest image dimensions from list of images

开发者 https://www.devze.com 2023-01-12 15:23 出处:网络
I have a list (the paths) of images saved locally. How can I find the largest image from these? I\'m not referring to the fi开发者_运维问答le size but the dimensions.

I have a list (the paths) of images saved locally. How can I find the largest image from these? I'm not referring to the fi开发者_运维问答le size but the dimensions.

All the images are in common web-compatible formats — JPG, GIF, PNG, etc.

Thank you.


Assuming that the "size" of an image is its area :

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)


Use Python Imaging Library (PIL). Something like this:

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)

Update (Thanks delnan):

Replace last two lines of above snippet with:

max(Image.open(f, 'r').size for f in filenames)

Update 2

The OP wants to find the index of the file corresponding to the largest dimensions. This requires some help from numpy. See below:

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


You will need PIL.

from PIL import Image

img = Image.open(image_file)
width, height = img.size

Once you have the size of a single image, checking the whole list and selecting the bigger one is not the problem.


import Image

src = Image.open(image)
size = src.size 

size will be a tuple with the image dimensions (witdh and height)

0

精彩评论

暂无评论...
验证码 换一张
取 消