2

How can I get the lowest height out of all heights and also the lowest width out of all width given I have about 50k images?

[jalal@goku images]$  identify -format '%w %h' 72028059_11.jpg
600 431

This command only gives w and h of an image.

I got this from IRC Linux channel but because I have 50k images it is taking forever to output any results:

[jalal@goku images]$ find -type f -name \*.jpg -exec identify -format '%w %h %d/%f\n' {} \; | sort -n -k2
  • Wait, you asked for the minimum. Edited. – Jacob Vlijm Nov 20 '17 at 20:45
  • Running -exec ... {} \; with 50k files is insane. Just using {} + should speed things up. – muru Nov 21 '17 at 8:35
3

Getting the image with min height and width

I don't have any comparing statistics, but I have reasons to believe the script below offers a relatively good option, since:

  • python's PIL does not load the image into memory when calling .open
  • The script itself does not store the list of all files, it simply looks per file if the next one has a smaller height or width.

The script

#!/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)

How to use

  1. Copy the script into an empty file, save it as get_minsize.py
  2. Run it with the directory of images as argument:

    python3 /path/to/get_maxsize.py /path/to/imagefolder
    

Output like:

minwidth (520, '/home/jacob/Desktop/caravan/IMG_20171007_104917.jpg')
minheight (674, '/home/jacob/Desktop/caravan/butsen1.jpg')

NB

The script assumes the image folder is a "flat" directory with (only) images. If that is not the case, a few lines need to be added, just mention.

-1

This worked for me:

$ find -type f -name \*.jpg -exec identify -format '%w %h %d/%f\n' {} + | sort -n -k1 > sorted_width
$ sort -k 1rn sorted_width
$ find -type f -name \*.jpg -exec identify -format '%w %h %d/%f\n' {} + | sort -n -k2 > sorted_height
$ sort -k 2rn sorted_height
  • 2
    Rather than just specifying commands, perhaps you can explain how each actually does what the user wants it to do? – Thomas Ward Nov 20 '17 at 23:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.