2010-08-20 20 views

回答

3

使用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) 

更新(感謝delnan):

上面代碼的最後兩行替換爲:

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

更新2

的OP希望找到最大dim對應的文件索引ensions。這需要numpy的一些幫助。請看下圖:

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

這確實應該'MAX(Image.open(F, 'R')大小爲F。在文件名中)'(生成器表達式!)。 – delnan 2010-08-20 09:32:20

+0

感謝Manoj。我喜歡使用lambda函數的你的方法。在這個例子中,我是否也可以獲得最大圖像的索引/位置? 我總是可以寫一個'for'循環遍歷整個事物,但我的方法並不像您的那樣簡潔。 – 2010-08-20 09:39:10

+0

@Mridang:恩,拉姆達?你不是指列表理解嗎? – delnan 2010-08-20 09:43:39

-1
import Image 

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

規模將與圖像尺寸(witdh和高度)

0

您需要PIL的元組。

from PIL import Image 

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

一旦你有一個圖像的大小,檢查整個列表並選擇最重要的原因不是問題。

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)