I have a very big collection of very big images, and I want to create a thumbnail gallery in which images are resized to a maximum-minimum-width (you will understand) of 180px.

For example: If the image is 2400x1800, it will be resized to 240x180 Else if it is 1600x1800, it will be resized to 180x202

Is it clear? The smallest size of the picture should be exactly 180, no matter if it is the width or height.

How is this possible with the command line? I know how to resize each file to a certain size, but not with those different sizes.

1 Answer 1


How about using ImageMagick's convertwhich has exactly such a minimal size option, see Image Geometry options?!

Copy & Paste snippet (bash syntax) -- please notice the ^ after the size specification:

for file in *.jpg; do
   echo -n Converting ${file}...
   convert -resize 180x180^ "$file" "th_$file"
   echo done

In addition, if you want to crop the resulting file to a quadratic shape around the center, you can use this script. The SIZE parameter in the first line specifies (surprise, surprise) the final size of the thumbnail.

for file in *.jpg; do
   echo -n Converting ${file}...
   convert -resize ${SIZE}x${SIZE}^ "$file" temp.png
   convert -crop $(identify temp.png | awk -F'[ x]' -v SIZE=$SIZE '{ printf "%ux%u+%u+%u", SIZE, SIZE, ($3-SIZE)/2, ($4-SIZE)/2 }') temp.png "th_$file"
   echo done
rm temp.png

The script is not very optimized, as it runs two commands (identify and convert -crop) on the thumbnail. But as the thumbnail is only small, I think the speed is reasonable.

  • Working like a charm! Additionally, is it possible to crop/cut an image in the center? Eg: it it's 240x180, then I cut 30px out of each side, for the image to be 180x180.
    – ranieri
    Commented Nov 16, 2013 at 23:25
  • @ranisalt I added a scripting solution for that job, too. But please extend you question accordingly, so that Q & A is consistent ;)
    – mpy
    Commented Nov 19, 2013 at 21:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .