如何按图像高度对图像列表进行排序?(蟒蛇)



如何按图像高度对图像列表进行排序? 列表例如:

list = ['img1.png', 'img2.png','img3.png']
The height of img1 is 150
The height of img2 is 75
The height of img3 is 329

预期结果:

list = ['img3.png','img1.png','img2.png']

提前感谢!

您可以使用内置函数"sorted"并按数组长度排序来对它们进行排序,数组应按行数(高度(排序。为 reverse 关键字传入 True 将允许您按降序排序。

list = sorted(lst, key=lambda x: len(x), reverse=True)

生成图像列表并按高度排序,如下所示:

import glob
from PIL import Image
# Generate a list of PNG files
filenames = glob.glob("*.png")
# Sort that list by height of image
byHeight = sorted(filenames, key=lambda x: Image.open(x).height)

最新更新