我有一份图像列表(路径),它们都存储在本地。如何从这些图像中找到最大的图像?我不是指文件大小,而是指尺寸。
所有这些图像都是普通网络兼容格式——JPG、GIF、PNG等。
谢谢。
所有这些图像都是普通网络兼容格式——JPG、GIF、PNG等。
谢谢。
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)
使用 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想要找到对应最大尺寸的文件的索引。这需要一些来自numpy
的帮助。请参见下文:
from numpy import array
image_array = array([Image.open(f, 'r').size for f in filenames])
print image_array.argmax()
from PIL import Image
img = Image.open(image_file)
width, height = img.size
一旦你获得了单个图像的大小,检查整个列表并选择更大的那一个就不是问题。
import Image
src = Image.open(image)
size = src.size
size将是一个元组,其中包含图像的尺寸(宽度和高度)
max(Image.open(f, 'r').size for f in filenames)
(生成器表达式!)。 - user395760largest = max(sizes)、sizes.index(largest)
来获取索引,这会被认为是笨拙的吗?这将在 sizes 数组中给出具有最大值的索引。由于第二个数组是通过对第一个数组进行迭代而构建的,因此索引将同步,并且第二个数组中最大值的索引将对应于第一个数组中图像的索引。你对此有什么看法?我没有处理多个图像具有相同尺寸的情况。 - Mridang Agarwalla