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
- 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.
from PIL import Image
path = sys.argv
# 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
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
if h < minh:
newminh = h, image_location
minh = h
# finally, print the values and corresponding files
How to use
- Copy the script into an empty file, save it as
Run it with the directory of images as argument:
python3 /path/to/get_maxsize.py /path/to/imagefolder
minwidth (520, '/home/jacob/Desktop/caravan/IMG_20171007_104917.jpg')
minheight (674, '/home/jacob/Desktop/caravan/butsen1.jpg')
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.