简体   繁体   中英

Find the largest image dimensions from list of images

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 file 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM