查找目录中所有图像中的最低高度和最低宽度

Mon*_*lal 3 command-line scripts imagemagick image-processing

鉴于我有大约 50k 图像,如何获得所有高度中的最低高度以及所有宽度中的最低宽度?

我尝试了这个命令,但它只给出了图像的宽度和高度:

identify -format '%w %h' 72028059_11.jpg
600 431
Run Code Online (Sandbox Code Playgroud)

我也从 IRC Linux 频道得到了这个,但是,因为我有 50k 图像,所以需要很长时间才能输出任何结果:

find -type f -name \*.jpg -exec identify -format '%w %h %d/%f\n' {} \; | sort -n -k2
Run Code Online (Sandbox Code Playgroud)

Jac*_*ijm 5

获取具有最小高度和宽度的图像

我没有任何比较统计数据,但我有理由相信下面的脚本提供了一个相对较好的选择,因为:

  • python的PIL调用时不会将图像加载到内存中.open
  • 脚本本身不存储所有文件的列表,它只是查看每个文件是否下一个文件具有较小的高度或宽度。

剧本

#!/usr/bin/env python3
from PIL import Image
import os
import sys

path = sys.argv[1]
# set an initial value which no image will meet
minw = 10000000
minh = 10000000

for image in os.listdir(path):
    # get the image height & width
    image_location = os.path.join(path, image)
    im = Image.open(image_location)
    data = im.size
    # if the width is lower than the last image, we have a new "winner"
    w = data[0]
    if w < minw:
        newminw = w, image_location
        minw = w
    # if the height is lower than the last image, we have a new "winner"
    h = data[1]
    if h < minh:
        newminh = h, image_location
        minh = h
# finally, print the values and corresponding files
print("minwidth", newminw)
print("minheight", newminh)
Run Code Online (Sandbox Code Playgroud)

如何使用

  1. 将脚本复制到一个空文件中,另存为get_minsize.py
  2. 使用图像目录作为参数运行它:

    python3 /path/to/get_maxsize.py /path/to/imagefolder
    
    Run Code Online (Sandbox Code Playgroud)

输出如下:

minwidth (520, '/home/jacob/Desktop/caravan/IMG_20171007_104917.jpg')
minheight (674, '/home/jacob/Desktop/caravan/butsen1.jpg')
Run Code Online (Sandbox Code Playgroud)

注意

该脚本假设图像文件夹是一个包含(仅)图像的“平面”目录。如果不是这样,需要添加几行,只需提及即可。