Find the largest image sizes from the image list

I have a list (paths) of images saved locally. How can I find the largest image of them? I do not mean file size, but sizes.

All images are in common Internet compatible formats - JPG, GIF, PNG, etc.

Thank.

+3
source share
4 answers

Use the Python Imaging Library (PIL). Something like that:

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 the last two lines of the above snippet as follows:

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

Update 2

OP , . numpy. . :

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

, "" - :

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

PIL.

from PIL import Image

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

Once the size of one image, checking the entire list and choosing a larger size is not a problem.

0
source
import Image

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

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

-1
source

Source: https://habr.com/ru/post/1760705/


All Articles